Reputation: 10750
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 boundTogether_projects
those projects that are bound together.
So inside the Project.php Model I created the following Eloquent relationships:
<?php
/*** 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?
Upvotes: 1
Views: 9535
Reputation: 111899
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) {
dd($project->boundTogetherProjects());
});
Upvotes: 3