[Request] Allow Eloquent to save multiple records at once
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.
+1.
I feel User::create([array of multiple user detail arrays]) would be a more appropriate function to handle this
But that isn't what an ORM does?
+3 (pun intended get it? I just bulk voted...)
What's wrong with
foreach ($users as $user)
{
User::create($user);
}
It executes as much queries as you have entries to add ?
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 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.
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.
Then why not DB::table('users')->saveMany($users);
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.
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.
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 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).
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...
+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 :)
+1 I really need to insert/update many thousand records, as fast as possible
Then use the DB:: methods.
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.
+1
+1 our users upload products in bulk (csv)