【发布时间】:2013-09-19 15:59:46
【问题描述】:
假设我有这两个类:
class User extends Eloquent
{
public function phone()
{
return $this->hasMany('phone');
}
}
class Phone extends Eloquent
{
public function user()
{
return $this->belongsTo('user');
}
}
(关系不一定以这种方式呈现。)
我正在尝试通过使用accessors 来实现这一点:
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 做这样的事情?
【问题讨论】:
-
您需要完成什么?我在您的代码中看不到任何错误,但是您需要关系背后的什么对象?
-
@AntonioCarlosRibeiro 我正在尝试通过访问器函数访问电话所属的特定用户(
getNameAttribute)
标签: php laravel laravel-4 eloquent