假设我有这两个类:
class User extends Eloquent
{
public function phone()
{
return $this->hasMany('phone');
}
}
class Phone extends Eloquent
{
public function user()
{
return $this->belongsTo('user');
}
}
(关系不一定以这种方式呈现。)
我试图通过使用访问器来实现这一点:
class Phone extends Eloquent
{
public function user()
{
return $this->belongsTo('user');
}
public function getNumberAttribute($value)
{
//Grant access to the User object who has this Phone.
$userWhoOwnsThisPhoneNumber = /*SOMETHING*/;
/*For example, $userWhoOwnsThisPhoneNumber->phone should return THIS phone object. */
//...
}
}
然后也许做这样的事情:
$user = User::find($id)->phone->number;
我多次浏览文档,我找不到任何有用的东西。可以用 Laravel 4 做这样的事情吗?