Skip to content

All topics  /  Laravel

How To Update All Rows Database Records in Laravel?

Laravel ·

Teams applying “How To Update All Rows Database Records in Laravel?” in a production project can use the implementation notes to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

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.");
    }
}