Support the ongoing development of →
Database Eloquent Architecture
Last updated 2 years ago.

The solution I implemented for now looks like this in my user class:

public function projects(){
    $builder = $this->belongsToMany('App\Project','user_in_project_group');
    $builder->getQuery()->getQuery()->distinct = TRUE;
    return $builder->get();
public function projectGroups($project_id){
    return $this->belongsToMany('App\Group', 'user_in_project_group')
         ->wherePivot('project_id', $project_id)->get();
public function groupProjects($group_id){
    return $this->belongsToMany('App\Project', 'user_in_project_group')
        ->wherePivot('group_id', $group_id)->get();

This way I get always the values I need. On the downside, I always have to use brackets to access the values in my views. On top of that, my functions return collections, so if I want to use stuff like sync(), attach() or detach(), I need to implement additional functions which return a Relation object.

Last updated 9 years ago.

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.