Sep 25, 2020
0
Level 54
Retrieving a morphOne model instance
Any suggestions on how I can get the group model returned when I am trying to retrieve the currentGroup off of a model?
When I try and run $student->currentGroup I am getting an SQL error stating that the column member_id does not exist on the groups table. I understand the error however that member_idis inside of the Pivot table calledgroup_members`.
/**
* Get the groups the model has been belonged to.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function groups()
{
return $this->morphToMany(Group::class, 'member', 'group_members');
}
/**
* Get the current group the member belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function currentGroup()
{
return $this->morphOne(Group::class, 'member')
->wherePivot('joined_at', '<=', now())
->wherePivot('left_at', '=', null)
->limit(1);
}
students
______
id
name
teachers
______
id
name
groups
______
id
name
group_members
______
id
group_id
member_id
member_type
Please or to participate in this conversation.