Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 1 year ago.
0

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.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.