How to Update Multiple Records in Laravel 9?
This tutorial will give you an example of how to update multiple rows and records in laravel eloquent. I will show you simply how to update multiple row in laravel. I will guide you to give an example of laravel update multiple records by id. From this tutorial, you will learn laravel update multiple rows with array also.
You can use this code in your laravel 6, laravel 7, laravel 8 and laravel 9 versions. You know that If we want to update multiple rows in laravel eloquent then we can use where() with update(), whereIn() with update() method of eloquent. I will show three simple examples to update multiple records in laravel eloquent.
Example 1
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
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:
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.");
}
}
Read also: Laravel 9 Restrict User Access From Specific IP Address
Hope it can help you.