Given this table structure:
I have a members
relation on my model.
public function members() {
return $this->belongsToMany('App\User', 'group_members')->withPivot('status', 'right_rid')->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 don't 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.
You could easily make a GroupMember
model and do something like the following...
GroupMember::where('user_id', Auth::user()->id)->where('group_id', $group_id)->with(['rights', 'groups', 'users'])->get();
This assumes you have a rights
, groups
, and users
method all in your GroupMember
model. Each of these should be a belongsTo
.