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

MattB's avatar
Level 2

How do populate pivot with extra info

I've always struggled with getting my head around pivot tables. I have 3 tables: users, conditions, and condition_user. The condition_user table has extra columns (years, months, days). How would I fill out the pivot table when a new user submits a condition (they may have more than one condition to input) alongside filling out the extra columns? I have no idea where to begin

0 likes
4 replies
a4ashraf's avatar

@mattb

you can get a pivot table date by this

return $this->belongsToMany('App\Models\Role')->withPivot('column1', 'column2')-> withTimestamps();

even you can change the pivot attribute name

return $this->belongsToMany('App\Models\Podcast') ->as('subscription') ->withTimestamps() ->withPivot('column1', 'column2');

MattB's avatar
Level 2

It wasn't about reading the data just yet, I want to know how to populate the pivot table with the extra information along side the 2 IDs

Please or to participate in this conversation.