It's so much easier to do that what you have.
call your pivot table group_user (singular and alphabetic order - this is the convention so you don't have specify your foreign keys)
use this code
User.php
function groups()
{
return $this->belongsToMany('Group');
}
Group.php
function users()
{
return $this->belongsToMany('User');
}
// to get all groups of a user
$user = User::find(1);
foreach ($user->groups as $group) {
echo $group->name;
}
I renamed the pivot table and adjusted Models but i prefer to demand the "dirty job" to the HomeController instead of doing a foreach directly in the view...
That's fine. it was just an example. it should still work the same way
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community