Php Laravel-2x在另一个表中有许多关系
我有表Php Laravel-2x在另一个表中有许多关系,php,laravel,eloquent,Php,Laravel,Eloquent,我有表消息 带有列from和to 对应于表用户中的某些用户的 所以我需要这样的东西(在用户模型中有很多关系-有很多消息) 但是有两个返回,第二个返回是一个无法访问的语句。 那么,当存在多个关系时,如何管理多个关系?创建第二个关系: public function messageRetrieved() { return $this->hasMany('App\Models\Messages', 'from', 'id'); } public function messageSend
消息
带有列from
和to
对应于表用户中的某些用户的
所以我需要这样的东西(在用户模型中有很多关系-有很多消息)
但是有两个返回,第二个返回是一个无法访问的语句。
那么,当存在多个关系时,如何管理多个关系?创建第二个关系:
public function messageRetrieved()
{
return $this->hasMany('App\Models\Messages', 'from', 'id');
}
public function messageSend()
{
return $this->hasMany('App\Models\Messages', 'to', 'id');
}
首先,您不能像这样使用2 return
public function Messages()
{
return $this->hasMany('App\Models\Messages', 'from', 'id');
return $this->hasMany('App\Models\Messages', 'to', 'id');
}
如果将其分为两种方法,效果会更好,如下所示:
public function to()
{
return $this->hasMany('App\Models\Messages', 'to', 'id');
}
public function from()
{
return $this->hasMany('App\Models\Messages', 'from', 'id');
}
public function to()
{
return $this->hasMany('App\Models\Messages', 'to', 'id');
}
public function from()
{
return $this->hasMany('App\Models\Messages', 'from', 'id');
}