vendredi 29 avril 2016

Laravel 5.2 - Multiple relations

This is my first Laravel 5 project.

DB tables Image

I have a 'members'-function in my Group controller with the following code:

public function members() {
    return $this->belongsToMany('App\User', 'group_members')->withPivot('status', 'right_id')->withTimestamps();
}

When I call this function it will return the members of a specific function.

Now I want to call a function called 'rights' or something like that, which will return the right_id and the type of the 'rights' table in a single line of code.

Now when I want the right_id of a member I use this line:

$group->members->where('id', Auth::user()->id)->first()->pivot->right_id

But then I dont have the 'rights'-type because the right_id is in the 'group_members'-table.

I want it to work something like this:

$group->members->where('id', Auth::user()->id)->first()->rights

I hope that this is enough information to give me some advice, or even a solution for this problem.

I know this could also be done by making a custom query, but I like the way Laravel works.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire