Hard Spocker Hard Spocker - 15 days ago 5
MySQL Question

How to get the data of the inserted multiple rows in Laravel Eloquent ORM

So I manage to save multiple datas at once using laravel

model::insert(array(...));


example data

array (
0 =>
array (
'organization' => '1',
'status' => false,
'project' => '1',
'act_date' => '2016-11-23 08:19:06',
),
1 =>
array (
'organization' => '1',
'status' => false,
'project' => '1',
'act_date' => '2016-11-23 08:19:06',
),
)


What I want is to get the data, or the id at least of the inserted array. This data must also contain the multiple id's (
array({ data[0].id=>1 }, { data[1].id=>2 })
) corresponding to the number of data's inserted.

The problem with
model::insert(array(...));
is that it returns
true
when successful and
false
otherwise.

model::create(array(...));
returns the data of the inserted row, but doesnt support multiple row inserts.

Answer

I would really recommend you to use create() method if you want to get an ID of each inserted row.

But if you really want to use insert() you could try this (method has it's own disadvantages though and I wouldn't use it):

model::insert($array);
$lastIds = model::orderBy('id', 'desc')->take(count($array))->pluck('id');