27-Dec-2022
.
Admin
Hello Friends,
You'll find an example of how to update multiple rows in Laravel Eloquent in this post. I gave a clear explanation of how to update multiple rows in Laravel. By using a Laravel update multiple records by id example, we will assist you. You'll learn how to update several rows in Laravel using an array.
Eloquent's where() and update() and whereIn() and update() methods can be used to update multiple rows in a table. To update numerous products in Laravel Eloquent, I gave three straightforward examples. So let's look at the examples one by one:
Now let's start.
Install Laravel
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Example 1:
App\Http\Controllers\ProductController
<?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:
App\Http\Controllers\ProductController
<?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:
App\Http\Controllers\ProductController
<?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...
#Laravel