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

Bhojendra's avatar

Defining database tables and it's pivot table

Products are created by users. Products can be updated by other users as well. And I want to list them like below:

id     product      created_by      updated_by
1       milk            John Doe         John Doe
2       biscuit       Alex Tiger        John Doe
3       chocolate  John Doe         Alex Tiger

Now, how to define use pivot table?

products table
------------------
id    product     created_by      updated_by
1       milk           1                        1
2       biscuit      2                       1
3       chocolate 1                        2

pivot table
---------------
id    product_id     user_id

Or,

products table
------------------
id    product
1       milk 
2       biscuit
3       chocolate

pivot table
---------------
id    product_id     user_id    created_by   updated_by

It would be my pleasure if you could show me the code for view and the controller as well.

Please help me. Thanks.

0 likes
2 replies
bottelet's avatar

You already store the created_by and updated_by on products, would not mean there is any need to have it on the pivot_table, just seems messy, also I personally like to have a little disturbance as possible, which means you could also just remove the id column if you wish.

Bhojendra's avatar

created_by and updated_by values are id referencing to users table and when displaying I want to show user name with that id.

Please or to participate in this conversation.