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

xuuto's avatar
Level 2

laravel pivot table with extra data fiels

hi every one. i want to make laravel database tables from existing schema. but i confused is it ok to add pivot table with exta fields like data fields for:example role-start-date, and role-end-date. this is the image for the schema i want to create, as you can see apart from, roles table , and staffs table staff-role table is pivot table, but it has extra columns for dates to select. how can i make that pivot table with extra date fields that user can select from input date form. thanks. the image is the link below https://www.vertabelo.com/blog/a-database-model-for-gallery-or-museum-management/model.png

0 likes
5 replies
mabdullahsari's avatar

Yeah, it's perfectly fine to add additional columns to pivot tables.

If you want to retrieve those columns whenever your relationship is queried, you need to use a withPivot.

xuuto's avatar
Level 2

@mabdullahsari what about inserting those extra columns with form. how is that possible, as we know pivot tables we donot use forms for inserting data.

mabdullahsari's avatar
Level 16

@xuuto If you are talking about technicality, the attach method on BelongsToMany relations accept an array argument in which you can provide those additional columns.

Otherwise, I can't tell you how you want to get that data. It's up to you or the business.

1 like
mabdullahsari's avatar

@xuuto Good. Now capture the needed data from a form and persist them to the database..?

1 like

Please or to participate in this conversation.