Pivot table only return user_id & project_id

Published 2 months ago by GTHell

I have user that can has many projects and vice versa. I have table call user_project with a few column but when query using eloquent $user->projects->find(1)->pivot I only get user_id and project_id. Where is the rest of the column?

Best Answer (As Selected By GTHell)
tykus

Your relationship should define which pivot values to return:

public function projects()
{
    return $this->belongsToMany(Project::class, 'user_project')
        ->withPivot('extra', 'pivot', 'columns');
}

What are you trying to do whenever you chain find() onto the Collection returned by $user->projects?

tykus
tykus
2 months ago (617,070 XP)

Your relationship should define which pivot values to return:

public function projects()
{
    return $this->belongsToMany(Project::class, 'user_project')
        ->withPivot('extra', 'pivot', 'columns');
}

What are you trying to do whenever you chain find() onto the Collection returned by $user->projects?

GTHell

@tykus It's catcha situation when I skim through the document.

It return all the project that association.

Please sign in or create an account to participate in this conversation.