Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

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
0 likes
0 replies

Please or to participate in this conversation.