How to Update Multiple Records in Laravel?
Hi Friends,
This post will give you an example of how to update multiple rows in laravel eloquent. I explained simply about how to update multiple row in laravel. we will help you to give an example of laravel update multiple records by id. you will learn laravel update multiple rows with array.
You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.
If you want to update multiple rows in laravel eloquent then you can use where() with update(), whereIn() with update() method of eloquent. I added three simple examples to update multiple products in laravel eloquent. so let's see the one by one example:
Example 1:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
Product::where("type", 1)
->update(["color" => "red"]);
dd("Products updated successfully.");
}
}
Example 2:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$ids = [34, 56, 100, 104];
Product::whereIn("id", $ids)
->update([
'color' => 'blue',
'size' => 'XL',
'price' => 200
]);
dd("Products updated successfully.");
}
}
Example 3:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$ids = [34, 56, 100, 104];
Product::whereIn("id", $ids)
->update($request->all());
dd("Products updated successfully.");
}
}
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Last 10 Records in Laravel?
- How to Get Current Week Records in Laravel?
- Laravel Get Next and Previous Record with URL Example
- How to Restore Deleted Records in Laravel?
- Laravel Copy Record using Eloquent Replicate Example
- How to Get Last 7 Days Record in Laravel?
- How to Get Last 30 Days Record in Laravel?
- Laravel Eloquent Delete Record By ID Example
- How to Get Current Month Records in Laravel?
- How to Get Last Record from Database in Laravel?
- How to Get Soft Deleted Records in Laravel?
- How to Insert Multiple Records in Laravel?
- How to Check If Record Exists or Not in Laravel?
- How to Store Data in Cache in Laravel?