数据库中有3个MySQL表:
产品介绍:
id | name
Run Code Online (Sandbox Code Playgroud)
产品价格:
product_id | currency_id | price
Run Code Online (Sandbox Code Playgroud)
货币:
id | mark
Run Code Online (Sandbox Code Playgroud)
Laravel的Eloquent模型看起来像这样:
// Product.php
class Product extends Eloquent {
protected $table = 'products';
protected $primaryKey = 'id';
public function prices(){
return $this->hasMany('ProductPrice', 'product_id', 'id');
}
}
// ProductPrice.php
class ProductPrice extends Eloquent {
protected $table = 'product_prices';
public function currency(){
return $this->hasOne('Currency', 'id', 'currency_id');
}
}
// Currency.php
class Currency extends Eloquent {
protected $table = 'currencies';
protected $primaryKey = 'id';
}
Run Code Online (Sandbox Code Playgroud)
现在我需要以所有价格展示所有产品!我的代码看起来像这样:
$products = Product::with('prices')->get(); …Run Code Online (Sandbox Code Playgroud) 比方说,我在Laravel中有一个User模型,如下所示:
class User extends Eloquent implements UserInterface, RemindableInterface {
public static $rules = array(
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
'password_confirmation' => 'required|min:8'
);
...
}
Run Code Online (Sandbox Code Playgroud)
存储在模型中的规则将重复用于登录和注册表单,但是当不需要密码确认(例如登录表单)时会出现问题.并且可能存在许多应该改变规则的情况.
那么,有没有任何纯方法如何修改Laravel中不同情况的模型存储验证规则?我是否必须重新组织我的规则存储方法?
谢谢!