我已经用传统方式定义了我的模型关系,并且它一直运行良好。然而,我开始使用 Ardent 来简化验证,并注意到它具有定义 Eloquent 关系的有用功能。
https://github.com/laravelbook/ardent#relations
为了把它放在上下文中,我有一个用户模型,它属于一个位置(在本例中是地理)。
之前,我会使用:
public function location(){
return $this->belongsTo('Location', 'location');
}
我没有任何问题。改用 Ardent 的做法产生了:
protected static $relationsData = array(
'location' => array(self::BELONGS_TO, 'Location', 'foreignKey' => 'location')
);
它总是返回 null 而不是用户位置。
用户.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 位置:int(11) FK(locations.id)
*locations*
id: int(11) PK
longtext: text
我不知道为什么这现在不再返回任何东西,因为它是相同的只是不同的语法。
我可以回到 Laravel 的做法,但我更喜欢 Ardent 的语法。