Blog Detail

18

Oct
Update Multiple Laravel Model Records by using Single Query cover image

arrow_back Update Multiple Laravel Model Records by using Single Query

Laravel Mass Update is a fantastic package that helps to update multiple Laravel Model records, each with its own set of values, sending a single query to your database!

Installation

You can install the package via composer:

composer require iksaku/laravel-mass-update

Usage

In your model class, add the Iksaku\Laravel\MassUpdate\MassUpdatable trait:

use Illuminate\Database\Eloquent\Model;
use Iksaku\Laravel\MassUpdate\MassUpdatable;

class User extends Model
{
    use MassUpdatable;
    
    // ...
}

And that’s all! Your model is now ready to update multiple records with varying values in a single query!

Let’s take a look at some possible use cases for this new query:

Simple use case #1:

Update the values of multiple records.

Imagine that you have the following users table:

id name username
1 Jorge Gonzales iksaku
2 Gladys Martines gm_mtz

But, we want to update both records since those users have told us that their legal last name was misspelled:

  • González is written with an accent on the letter a, and only uses z, never an s.
  • Martínez is written with an accent on the letter i, and the last letter should be a z, not an s
Well, we can mass-update those specific records:

User::massUpdate(
    values: [
        ['id' => 1, 'name' => 'Jorge González'],
        ['id' => 2, 'name' => 'Gladys Martínez'],
    ]
);

Now, both records will be updated with their corresponding values in a single query, resulting in:

id name username
1 Jorge González iksaku
2 Gladys Martínez gm_mtz

By default, the massUpdate query will grab your model’s primary key name and apply it as part of the query to not affect other records.

Simple use case #2:

Updating multiple Eloquent Models

If you need to update the values in some Model classes and want to automatically mass update those changes, then this is for you!

The existing masUpdate query is capable of identifying the dirty attributes of Eloquent model classes and compile them properly. You don’t need to manually convert the models into an array, you just pass the list of models you want to update, and it takes care of the rest.

Let's recreate the previous example, but using Eloquent models...

// Say we already pulled our user models previously... Something like this:
$jorge = User::where('name', 'Jorge Gonzales')->first();
$gladys = User::where('name', 'Gladys Martines')->first();

// And let's say we already made changes to those models... Like this:
$jorge->name = 'Jorge González';
$gladys->name = 'Gladys Martínez';

// And now, let's update both models in a single query:
User::massUpdate(
    values: [$jorge, $gladys]
);

This package has many other use cases with code examples, For more details and source code, please visit Github.

Note

If you own a business and want to use Laravel for your next PHP-based web application, CRM Software, E-commerce application, SAAS application, or any custom web application development for your custom requirements, you must find an impeccable team with experience in the Laravel framework. So Codebrisk is here to help you with your tailored requirements regarding Laravel Development. For further assistance, Please feel free to send us an email at rizwan@codebrisk.com or get in touch with us, our business person will get back to you.

Published at : 18-10-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project