Add this to your project model - it checks that the Collection of users returned by
the belongsToMany relationship method contains the given user
// Project.php
public function hasUser($user) {
return $project->users->contains($user);
}
Hi,
I have two models : User and Project. There is a "Many To Many" relationship between them.
I have a fonction in a controller which retrieves a project by his token :
$project = Project::getByToken($request->tokenProject)->firstOrFail();
But before returning the collection, i want to check if the current user and the collection are related (Means that both are connected in the "project_user" table). So i would like to do something like that :
$project = Project::getByToken($request->tokenProject)->firstOrFail();
if($project->hasUser(Auth::id())
{
return $project;
}
Is there a function which can actually check that ? I searched in every topics but without success...
Add this to your project model - it checks that the Collection of users returned by
the belongsToMany relationship method contains the given user
// Project.php
public function hasUser($user) {
return $project->users->contains($user);
}
Please or to participate in this conversation.