pathros pathros - 1 month ago 7
PHP Question

Eloquent Fatal error: Call to undefined method Eloquent\Collection::addEagerConstraints() in \illuminate\database\Eloquent\Builder.php on line 451

I am following a tutorial where you can save relationships in one table to determine if a user is friend of another user and viceversa.

In my case, I need to save in a relational table

those projects that are bound together.

So inside the Project.php Model I created the following Eloquent relationships:

/*** Bound together projects ***/
public function projectsBoundToThisOne(){
return $this->belongsToMany('Models\User\Project', 'boundTogether_projects', 'bound_id', 'project_id');
public function boundTo(){
return $this->belongsToMany('Models\User\Project', 'boundTogether_projects', 'project_id', 'bound_id');
public function boundTogetherProjects(){
return $this->projectsBoundToThisOne()->wherePivot('bound',true)->get()->merge($this->boundTo()->wherePivot('bound', true)->get());

This is not working, since I get the following error:

Fatal error: Call to undefined method
Illuminate\Database\Eloquent\Collection::addEagerConstraints() in
\illuminate\database\Eloquent\Builder.php on line 451

Looks like the problem comes right when I call this method as eager loading. That is to say:

$projects = Project::with('boundTogetherProjects');

The Eloquent version is 5.1

What am I missing? How do I fix it?


This is probably because boundTogetherProjects is not relationship method. It simple gets some data from relationship merged to the other. So the valid way would be:

$projects = Project::with('projectsBoundToThisOne','boundTo');

and then for each project you could run boundTogetherProjects to get the projects

$projects->each(function($project) {