Categories
Laravel how to insert data into intermediate table?
When linking many to many, you need to insert data into 2 tables at once, are there any convenient ways in Laravel? Like this
$user->roles()->create($dataNewRole, dataNewPivot);
Answer the question
In order to leave comments, you need to log in
The second argument is an array of fields that is inserted into pivot. Here is an example from my code:
$lead->contacts()->save($contactModel, ['is_main' => array_get($contact, 'is_main', false), 'is_lpr' => array_get($contact, 'is_lpr', false)]);
Didn't find what you were looking for?
Ask a Question
731 491 924 answers to any question