【发布时间】:2015-10-04 21:07:36
【问题描述】:
我在User和Credit之间建立了一对多的Eloquent关系
用户
id | name
信用
id | recipient | author | amount
在哪里
recipient 是user.id
author 是user.id
在 Laravel 中,recipient 的关系已建立:
class Credit extends Model {
public function user() {
return $this->belongsTo('App\User', 'recipient');
}
}
但是我该如何添加author 的关系呢?
我试过了:
class Credit extends Model {
public function recipient() {
return $this->belongsTo('App\User', 'recipient');
}
public function author() {
return $this->belongsTo('App\User', 'author');
}
}
即我把user()改成了两个函数:recipient()和author()
但我得到了Trying to get property of non-object,我认为这意味着使用recipient() 或author(),Eloquent 无法将其“绑定”到我的User 模型。
我该如何解决这个问题?
【问题讨论】: