BadMethodCallException方法Illuminate \ Database \ Query \ Builder :: merge不存在

时间:2018-07-15 23:40:04

标签: laravel laravel-5 eloquent

获取 BadMethodCallException,方法Illuminate \ Database \ Query \ Builder :: merge不存在。。尝试执行以下命令

public function getFriends1()
{
    return $this->belongsToMany('App\Friend', 'friends', 'user_id', 'friend_id');
}

public function getFriends2()
{
    return $this->belongsToMany('App\Friend', 'friends', 'friend_id', 'user_id');
}

public function getFriends()
{
    return $this->getFriends1()->merge($this->getFriends2());
}

2 个答案:

答案 0 :(得分:0)

您可以做两件事:

// Option 1
public function getFriends()
{
    return $this->getFriends1->merge($this->getFriends2);
}

// Option 2
public function getFriends()
{
    return $this->getFriends1()->get()->merge($this->getFriends2->get());
}

答案 1 :(得分:0)

在定义的关系中添加“-> get()”:

public function getFriends()
{
    $friends1= $this->belongsToMany('App\Friend', 'friends', 'user_id', 'friend_id')->get();
    $friends2= $this->belongsToMany('App\Friend', 'friends', 'friend_id', 'user_id')->get();
    return $friends1->merge($friends2);
}