我的传统方式定义了我的模型关系,并且工作正常。但是,我开始使用Ardent使验证更容易,并注意到它有一个用于定义Eloquent关系的有用功能。
https://github.com/laravelbook/ardent#relations
为了把它放到上下文中,我有一个User模型,它属于一个Location(在这个例子中是地理位置)。
以前,我会用:
public function location(){
return $this->belongsTo('Location', 'location');
}
我没有任何问题。转而采用热心的方式产生了:
protected static $relationsData = array(
'location' => array(self::BELONGS_TO, 'Location', 'foreignKey' => 'location')
);
始终返回null而不是Users位置。
user.php的
use LaravelBook\Ardent\Ardent;
class User extends Ardent implements UserInterface, RemindableInterface {
protected static $table = 'agents';
protected static $relationsData = array(
'location' => array(self::BELONGS_TO, 'Location', 'foreignKey' => 'location')
);
}
数据库 剂 id:int(11)PK location:int(11)FK(locations.id)
*locations*
id: int(11) PK
longtext: text
我不知道为什么现在不再返回任何东西,因为它只是不同的语法。
我可以回到Laravel这样做的方式,但我更喜欢Ardent的语法。
答案 0 :(得分:0)
列名称/关系冲突的简单情况。