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

Hard Spocker

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.

Alexey Mezenin

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');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Eloquent ORM laravel 5 Get Array of ids

Get the Last Inserted Id Using Laravel Eloquent

Laravel 4: how to "order by" using Eloquent ORM

Automatically deleting related rows in Laravel (Eloquent ORM)

How to add multiple where clause on Eloquent ORM Laravel?

How to get last insert id in Eloquent ORM laravel

laravel Eloquent ORM multiple table insert

How to sort Laravel Eloquent ORM objects?

Laravel get last inserted data

How to update multiple rows in sql from array data using laravel eloquent

How to get last id inserted on a database with eloquent

Laravel 5.6 get all rows as arrays of values using Eloquent ORM

Laravel eloquent get the latest rows of grouped rows

How to get inserted IDs of multiple rows on one INSERT?

Laravel Eloquent ORM - How to get the all the properties of afrom/of a collection in a array?

Retrieving relationship data in Eloquent ORM (Laravel PHP)

How to get data from Multiple Tables with Laravel Eloquent

How to get last rows with group by in Laravel eloquent

Querying multiple table to get specific data with Eloquent ORM

Laravel 4 : How to combine where and with in eloquent ORM

How to get multiple rows data of the form in controller in laravel 5.1

Laravel Eloquent ORM method for Retrieving Multiple Rows based on an Array of ids

Laravel Eloquent ORM - Get first and third table data

How to get inserted id of pivot when I use attach() in Laravel 5.6 (Eloquent)?

how to retrieve multiple table data with multiple criteria in laravel eloquent?

How to get data from database based on Many to many relation in Laravel Eloquent ORM

Get data from multiple table in Laravel 5.7 (Eloquent)

Laravel Eloquent ORM error with multiple database connections

How do i get all rows with max value in laravel eloquent?