Inside this article we will see the concept i.e Laravel 9 How To Update Multiple Records Example Tutorial. Article contains the classified information about How to Update Multiple Records in Laravel 9.
If you are looking for a solution i.e Update multiple records using Laravel Eloquent then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
Read More: Laravel 9 Language Translation Methods Example Tutorial
To update multiple rows in laravel eloquent then you can use where() with update(), whereIn() with update() method of eloquent.
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Example 1: Use of where() with update() Method
Suppose we have ProductController.php a controller file. We will use where() & update() method.
<?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" => "Dark blue"]); dd("Products updated successfully."); } }
Here,
Read More: JavaScript How To Scroll To Specific Element with Animation
From above query all products will be update whose type = 1.
Example 2: Use of whereIn() with update() Method
Suppose we have ProductController.php a controller file. We will use whereIn() & update() method to update products in bulk.
<?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."); } }
Here,
From above query all products will be update whose ids is of either [34, 56, 100, 104].
Example 3: Use of whereIn() with update() Method (Request Object)
Suppose we have ProductController.php a controller file. We will use whereIn() & update() method to update products in bulk.
We will get update data from request.
<?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."); } }
Here,
From above query all products will be update whose ids is of either [34, 56, 100, 104].
Read More: How To Work with External API in Laravel 9 Tutorial
We hope this article helped you to learn Laravel 9 How To Update Multiple Records Example Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.