framework icon indicating copy to clipboard operation
framework copied to clipboard

[Request] Allow Eloquent to save multiple records at once

Open roggel2000 opened this issue 13 years ago • 56 comments

I'd love to be able to insert multiple records into the database in one fell swoop. I know you can do that with something like User::insert([array of multiple user detail arrays]); but insert is not handled by Eloquent, but by Fluent instead.

One of the disadvantages of that, is that Fluent doesn't set timestamps on these records.

roggel2000 avatar May 14 '13 11:05 roggel2000

+1. I feel User::create([array of multiple user detail arrays]) would be a more appropriate function to handle this

abishekrsrikaanth avatar May 14 '13 12:05 abishekrsrikaanth

But that isn't what an ORM does?

robclancy avatar May 14 '13 12:05 robclancy

+3 (pun intended get it? I just bulk voted...)

jaketoolson avatar May 14 '13 16:05 jaketoolson

What's wrong with

foreach ($users as $user)
{
    User::create($user);
}

crynobone avatar May 14 '13 16:05 crynobone

It executes as much queries as you have entries to add ?

Anahkiasen avatar May 14 '13 16:05 Anahkiasen

I'd hope that with this request, Eloquent would (behind the scenes) create a single bulk extended insert, rather than multiple insert statements as a foreach() would.

jaketoolson avatar May 14 '13 16:05 jaketoolson

@jaketoolson Eloquent would still need to run the insert one by one. How would you expect the event to be fired if it's a bulk insert? As mention by @robclancy this isn't really a task for ORM.

crynobone avatar May 14 '13 16:05 crynobone

Well no that's the point, it doesn't need to run them one by one. Most SQL languages support bulk inserts, I'm pretty sure having one query instead of, say, 50, is significant enough to be added. And if you can insert multiple related models with saveMany I don't see why inserting multiple models in itself isn't in the scope.

Anahkiasen avatar May 14 '13 17:05 Anahkiasen

Then why not DB::table('users')->saveMany($users);

crynobone avatar May 14 '13 17:05 crynobone

saveMany() only works for relations (I believe many-to-many relations specifically), so something like this: $user->saveMany($roles) That's not what we're going for here.

roggel2000 avatar May 14 '13 17:05 roggel2000

It would be relatively easy to do this; however, events would not be fired. The only thing it would be buying you is the timestamp insertion.

taylorotwell avatar May 15 '13 00:05 taylorotwell

Just an idea but perhaps when performing multiple creates, deletes and updates the system could throw some 'creating.many' or 'updating.many' events that instead of passing along the actual object it passed an array of affected primary key ids. So you could handle that use case in your event handlers.

helmut avatar May 15 '13 01:05 helmut

@helmut then what if I would need to know certain attributes (added or changed) on the event? I would still need to fetch the row from database (and compared it against old value). Example:

static::updating(function ($role)
{
     Acl::renameRole($role->getOldAttributes('name'), $role->name);
});

With DB Query Builder, it would be an expected behaviour that event wouldn't be fired (at all).

crynobone avatar May 15 '13 01:05 crynobone

If timestamp insertion is the only thing it'd bring, I'm okay with setting those myself.

foreach( $users as $user ) {
    $user['created_at'] = new DateTime;
    $user['updated_at'] = $user['created_at'];
}
User::insert($users);

Something like that...

roggel2000 avatar May 16 '13 14:05 roggel2000

+1 i was just trying to do multi-array insert in L3 you know something you would think would happen by design and then when i got the error i researched old L3 docs and i needed to make a foreach loop :)

hasokeric avatar May 31 '13 07:05 hasokeric

+1 I really need to insert/update many thousand records, as fast as possible

drakakisgeo avatar Jul 01 '13 14:07 drakakisgeo

Then use the DB:: methods.

robclancy avatar Jul 02 '13 06:07 robclancy

Can I have something like |LOAD DATA |(Mysql) with DB::method? Topic here is to do as little queries as you can to insert/update many thousand records.

On 2/7/2013 9:14 πμ, Robert Clancy (Robbo) wrote:

Then use the |DB::| methods.

— Reply to this email directly or view it on GitHub https://github.com/laravel/framework/issues/1295#issuecomment-20328423.

drakakisgeo avatar Jul 02 '13 07:07 drakakisgeo

+1

jfloff avatar Jul 02 '13 21:07 jfloff

+1 our users upload products in bulk (csv)

pushpak avatar Jul 05 '13 14:07 pushpak