You need a model for your pivot to manage the second relation.
And maybe something like that to get the pivot last inserted id :
$user->roles()->latest()->first()->pivot->id
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello to all,
I have a big dilema. I have to entities that have a many to many relationship. But the pivot table has a many to many with another entity.
for example:
Users Roles user_role Tags
#id #id #user_id #id
#role_id
other data
The user_role has a many to many with tags. Have can I do this with Eloquent ? Should I create another Model for user_role ? If I do
$user->roles()->attach($role)
I need the that to return the id or somehow get the id and based on that I need to add tags to that relation.
Please or to participate in this conversation.