$member->groups gives you all the groups the $member is in.
Dec 12, 2018
6
Level 13
Model Alias - 'Members' Vs. 'Users'
I've a users table that contains to all the registered users of the site. There's a User model to represent them.
Now I'm build a 'Groupsapplication which is a feature of my site. While creating models for the 'Groups' application on my site; I created aMembermodel. Now,Member` looks logical for Groups; but essentially, it's referring to users on the site.
Now I'm stuck at defining relationships. For example, I've this -
class Member extends Model {
public function groups() {
return $this->belongsToMany(Group::class, 'groups_members', 'member_id', 'group_id')->withPivot('role');
}
}
While this is okay, how do I access it in my controller? Let's say I wish to fetch all the groups that I'm a part of. How'd I go about fetching the groups?
Would appreciate your support.
Please or to participate in this conversation.