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

MattB's avatar
Level 2

Pivot table query

So, I have an existing table of emotions that a logged-in user is asked to rate 1,2, or 3 from a form. Many users can have many emotions rated, so I was thinking about an emotion_user pivot table. My query is how to write this?

If I were to just write to a normal table called emotion_user using its own controller, would this act as a pivot table or would I have to write a special pivot table so Laravel can query it properly?

I'm writing a pivot if I am saving for example a post to a Posts table with Categories attached as this would create a pivot table of categories_posts and fill that in automatically, but is there a special process to follow if all I need is a pivot table?

Sorry, if I'm not explaining myself well

0 likes
3 replies
laracoft's avatar

Creating a pivot table and its model file is key. Controller not so much. You must have a emotion_user table with id, emotion_id and user_id. Normally this table is coded in a migration file.

automica's avatar

@laracoft yours is probably typo, but fields should be 'id', 'emotion_id' and 'user_id'

Please or to participate in this conversation.