chack1172/laravel-single-save

Optimize model saving with single save execution

1.0.0 2023-07-18 23:36 UTC

This package is auto-updated.

Last update: 2024-12-19 02:53:36 UTC


README

This package is designed to optimize the model saving process of your Laravel applications. Wrapping the code in a specific method callback performs a single database update at the end of the callback execution. This optimizes the application's performance, especially during complex transactions or scenarios with frequent model updates.

Installation

  1. Install the package using composer:
composer require chack1172/laravel-single-save
  1. Add the Eloquent trait to your models:
<?php
...
use Chack1172\SingleSave\Eloquent\SingleSave;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SingleSave;
}

Usage

Wrap all the queries inside singleSave method:

class User extends Authenticatable
{
    use SingleSave;

    public function updateName($name)
    {
        $this->name = $name;
        $this->save();
    }

    public function updatePassword($password)
    {
        $this->password = $password;
        $this->save();
    }
}

// ...
$user->singleSave(function ($user) {
    $user->updateName('Marco Rossi');
    $user->updatePassword('1234');
});

This code will run a single query when the callback is terminated updating both name and password in the database.

License

This project is licensed under the MIT License. Please see License File for more information.