laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

[Request] Allow Eloquent to save multiple records at once #1295

Closed rogierborst closed 11 years ago

rogierborst commented 11 years ago

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.

mityukov commented 6 years ago

To my previous answer: there's actually a ready-to-use solution available (MySQL): https://github.com/kfirba/import-query-generator

vschoener commented 6 years ago

@cabloo Looks like this is one of the best solutions we got :) I mean we need to Transform our data to collection and then save them, so :)

Isaac009 commented 6 years ago

This worked really well for me, I read multiple records from the cart table and save them into sells table ...

$carts = cart::all(); foreach($carts as $cart){ $sell = new sell; $sell->product_id = $cart->product_id; $sell->quantity = $cart->quantity; $sell->total = $cart->total; $sell->user_id = $cart->user_id;

        $sell->save();
    }
akr4m commented 5 years ago

Please check this from laravel official documentation

https://laravel.com/docs/5.7/eloquent-relationships#inserting-and-updating-related-models

:+1:

bulgariamitko commented 5 years ago

This worked for me, so i guess i would like to share it with you guys - https://stackoverflow.com/questions/54435692/how-to-save-many-records-in-a-table-laravel-eloquent-no-relationship/54436120

driesvints commented 5 years ago

I'm locking this to prevent this from becoming a forum thread and because the issue is so old.