Posted inTechnology / Laravel / Mysql / Mysqli / php / Programming

Laravel update multiple rows with different values

Today, We want to share with you Laravel update multiple rows with different values.In this post we will show you laravel eloquent update array, hear for How to update multiple row with different ID in Laravel 6, we will give you demo and example for implement.In this post, we will learn about laravel update multiple rows in one query with an example.

Laravel update multiple rows with different values

There are the Following The simple About laravel eloquent update multiple tables Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel update multiple rows with same values, so the Update multiple rows with different update values feature request is used for this example is following below.

How to update multiple row with different Collumns in Laravel 6,

DB::table('products')->where('product_id', $id)->where('ProductsName','ProductAdMaxImage')->update(['ProductDescValue' => $request->ProductAdMaxImage]);

DB::table('products')->where('product_id', $id)->where('ProductsName','ProductAdExpiredDay')->update(['ProductDescValue' => $request->ProductAdExpiredDay]);

Example 1: Update multiple rows of database in Laravel

$values = Cart::where('cart_id', $id)->update(['data'=>$data]);

Example 2 :Mass Updates records in Laravel Eloquent

App\CartTable::where('status', 2)
->where('destination', 'TamilRokers')
->update(['delayed' => 5]);

Example 3 :Update multiple records in Laravel Eloquent

$update_cart = Cart::where('cart_id', '=', $request->id)->update(['cart_id' => $request->cart_id]);
//$update_cart->save();

Example 4: Updating Multiple Records using Eloquent

Cart::whereIn('id', $ids)->update($request->all());
Web Programming Tutorials Example with Demo

How to Update Multiple Records in Laravel?

You can update multiple records in Laravel using the update method on the Eloquent model or the query builder.

Here is an example using the Eloquent model:

// Define an array of IDs to update
$ids = [1, 2, 3, 4];

// Define the new values to update
$data = [
    'status' => 'published',
    'published_at' => now(),
];

// Update the records
Model::whereIn('id', $ids)->update($data);

In this example, we use the whereIn method to select the records we want to update based on their IDs. Then, we use the update method to set the new values.

Here is an example using the query builder:

// Define an array of IDs to update
$ids = [1, 2, 3, 4];

// Define the new values to update
$data = [
    'status' => 'published',
    'published_at' => now(),
];

// Update the records
DB::table('table_name')->whereIn('id', $ids)->update($data);

In this example, we use the table method to specify the name of the table we want to update. Then, we use the whereIn method to select the records we want to update based on their IDs, and finally, we use the update method to set the new values.

Note that in both examples, we use the whereIn method to select multiple records to update. You can modify the query to select records based on any other condition.

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel update multiple columns.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype