chack1172 / laravel-single-save
Optimize model saving with single save execution
1.0.0
2023-07-18 23:36 UTC
Requires
- php: ^7.4|^8.0
- illuminate/database: ^8.0|^9.0|^10.0
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
- Install the package using composer:
composer require chack1172/laravel-single-save
- 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.