Reputation: 161
I have 3 tables:
user: id,name
group: id,name,parent_id
made with this) // this is a nested category
members: id,user_id,group_id,admin
and group_member: group_id,member_id
I want get all groups (include sub groups) for a member. Is it possible do it via laravel?
Group Modal:
public function parent()
{
$parent = $this->belongsTo('App\Group', 'parent_id');
return $parent;
}
public function children()
{
$children = $this->hasMany('App\Group', 'parent_id');
//$children->wherePublish(1);
return $children;
}
public function members()
{
return $this->hasMany(Member::class);
}
public function member()
{
return $this->belongsToMany(Member::class);
}
And Member model:
public function groups()
{
return $this->belongsToMany(Group::class);
}
public function group()
{
return $this->belongsTo(Group::class);
}
And user model:
public function members()
{
return $this->hasMany(Member::class);
}
I want something like this (to return all related groups):
auth()->user()->members()->groups()->get();
I got this error:
"Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::groups()"
Upvotes: 1
Views: 979
Reputation: 1
Please use this below query:
App/User::where('id',auth()->user()->id)->with('members.groups')->get();
Upvotes: 0