N
N
NubasLol2018-11-14 19:54:11
Laravel
NubasLol, 2018-11-14 19:54:11

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

1 answer(s)
2
2vtlk, 2018-11-22
@2vtlk

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 your question

Ask a Question

731 491 924 answers to any question