Bulk Insertion in Laravel Using Eloquent Orm

Bulk Insertion in Laravel using eloquent ORM

You can just use Eloquent::insert().

For example:

$data = [
['name'=>'Coder 1', 'rep'=>'4096'],
['name'=>'Coder 2', 'rep'=>'2048'],
//...
];

Coder::insert($data);

Laravel 6: Bulk Insert With Bulk Insert Relation

I have made some assumptions about the structure of your data, and table/column names, let me know if this achieves the bulk inserting you were looking for:

$itineraries = [];
$relatedMeals = [];

foreach ($request->product_itineraries as $product_itinerariesKey => $product_itineraries) {
if (empty($product_itineraries)) {
continue;
}

$itinerary['product_id'] = $product->id;
$itinerary['itinerary_title_en'] = $product_itineraries['itinerary_title_en'];
$itinerary['itinerary_title_th'] = $product_itineraries['itinerary_title_th'];
$itinerary['day_number'] = $product_itineraries['day_number'];
$itinerary['itinerary_description_en'] = $product_itineraries['itinerary_description_en'];
$itinerary['itinerary_description_th'] = $product_itineraries['itinerary_description_th'];
$itinerary['created_at'] = $nowTime;
$itinerary['updated_at'] = $nowTime;

$itineraries[] = $itinerary;
$relatedMeals[] = $product_itineraries['meal_type_id'];
}

DB::beginTransaction();

try {
$itineraryId = ProductItinerary::sharedLock()->max('id') + 1;
ProductItinerary::insert($itineraries);

$relations = [];
foreach ($relatedMeals as $mealIds) {
foreach ($mealIds as $mealId) {
$relations[] = [
'product_itinerary_id' => $itineraryId,
'meal_id' => $mealId,
];
}
$itineraryId++;
}

DB::table('product_itinerary_meal')->insert($relations);

DB::commit();
} catch(Exception $e) {
DB::rollback();
throw $e;
}

How to insert multiple rows from a single query using eloquent/fluent

It is really easy to do a bulk insert in Laravel using Eloquent or the query builder.

You can use the following approach.

$data = [
['user_id'=>'Coder 1', 'subject_id'=> 4096],
['user_id'=>'Coder 2', 'subject_id'=> 2048],
//...
];

Model::insert($data); // Eloquent approach
DB::table('table')->insert($data); // Query Builder approach

In your case you already have the data within the $query variable.

Best way to bulk create models within Laravel?

The for loop below solved this for me. It was originally posted by someone but its been deleted recently so ill answer the question below but thanks to whoever provided it!

$newVouchers = [];
for ($i = 0; $i < $request->voucher_amount; $i++) {
$newVouchers[] = VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
}

How to bulk insert in laravel 5.4 using eloquent

What is your error?

I think you can use request without input() like this:

 .....
$directcheque->client_id=$request->client_id;
....

Hope it helps :)

Laravel Eloquent - bulk update with whereIn array

Here is a good way to solve it:
in the beginning, I used this library: https://github.com/mavinoo/laravelBatch
to update many dynamic rows, but it was really slow, then thanks to Yasin, I moved to: https://github.com/iksaku/laravel-mass-update and now it works way better.

the implementation is simple, add a simple code to the Model class, then add:

User::massUpdate(
values: [
['username' => 'iksaku', 'name' => 'Jorge González'],
['username' => 'gm_mtz', 'name' => 'Gladys Martínez'],
],
uniqueBy: 'username'
);

while uniqueBy is the key for the row, and add other columns values to change them dynamically.



Related Topics



Leave a reply



Submit