├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── src ├── Commands │ └── MakeModelUpdater.php ├── ModelUpdaterServiceProvider.php ├── Traits │ └── Updatable.php └── Updater.php └── stubs └── ModelUpdater.stub /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Saleem Hadad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Model Updater 2 | 3 | - [Overview](#overview) 4 | - [Installation & Usage](#installation--usage) 5 | - [Contributing](#contributing) 6 | - [Security](#security) 7 | - [Credits](#credits) 8 | - [License](#license) 9 | 10 | 11 | ## Overview 12 | 13 | ModelUpdater is a package helps you define the business logic and the validation needed to update an entire model's fields or just a few of them via a simple controller action and request handler. 14 | 15 | Let's take a simple example to illustrate the purpose of this package: 16 | 17 | Suppose you have a user model and you have an API endpoint to update a user's fields in the database where as **the API consumer can update any field individually or all at once and the problem is that each field has its own logic needed before you store the updated value to the database.** 18 | 19 | 20 | Of course, there are a lot of different ways to handle this case, let's consider this simple and common one: 21 | 22 | > Old Way 23 | 24 | As you can see the controller's method with two fields only becomes a big one, just imagine if you have 20+ fields! 25 | 26 | ```php 27 | class UserController extends Controller 28 | { 29 | public function update(Request $request) 30 | { 31 | $user = auth()->user(); 32 | 33 | // now you check for every field you allowed to update and perform the needed logic. 34 | if($request->has('name')) { 35 | // needed logic and validation 36 | ... 37 | .. 38 | $user->name = $request->name 39 | } 40 | 41 | if($request->has('email')) { 42 | // needed logic and validation 43 | ... 44 | .. 45 | $user->email = $request->email 46 | } 47 | 48 | // update/save the model 49 | $user->save(); 50 | 51 | // return a response 52 | return response()->json(); 53 | } 54 | } 55 | ``` 56 | 57 | > New Way (Simpler) 58 | 59 | ```php 60 | //App/Http/Controllers 61 | use App\Updaters\UserUpdater; 62 | 63 | class UserController extends Controller 64 | { 65 | public function store(UserUpdater $updates) 66 | { 67 | auth()->user()->fillUpdates($updates); 68 | } 69 | } 70 | 71 | // App/Updaters 72 | class UserUpdater extends Updater 73 | { 74 | /** 75 | * Allowed fields to be updated. 76 | */ 77 | protected $fields = ['name', 'email']; 78 | 79 | protected function name($value) 80 | { 81 | $this->request->validate(['name' => 'required|string|min:6']); 82 | 83 | $this->model->name = $value; 84 | } 85 | 86 | protected function email($value) 87 | { 88 | $this->request->validate(['email' => 'required|string|min:6']); 89 | 90 | $this->model->email = $value; 91 | } 92 | } 93 | ``` 94 | 95 | ## Installation & Usage 96 | 97 | 1. Install the package via composer: 98 | 99 | > This package supports Laravel 5.5+ only 100 | 101 | ```bash 102 | composer require laimoon/model-updater 103 | ``` 104 | 105 | 2. Add `Updatable` trait to your desired model: 106 | 107 | ```php 108 | use Laimoon\UpdatableModel\Traits\Updateable; 109 | 110 | class User extends Authenticatable 111 | { 112 | use Updateable; 113 | } 114 | ``` 115 | 116 | 3. Make a new model updater: 117 | 118 | ```bash 119 | php artisan make:updater User 120 | ``` 121 | 122 | This command will generate a new directory under `app` with namespace of `Updaters` and a file inside it called (in this example) `UserUpdater.php`. 123 | 124 | 4. Make use in your controllers: 125 | 126 | ```php 127 | use App\Updaters\UserUpdater; 128 | 129 | class UserController extends Controller 130 | { 131 | public function store(UserUpdater $updates) 132 | { 133 | auth()->user()->fillUpdates($updates); 134 | 135 | return response()->json(); 136 | } 137 | } 138 | ``` 139 | 140 | ## Contributing 141 | 142 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 143 | 144 | ## Credits 145 | 146 | - [Saleem Hadad](https://github.com/saleem-hadad) 147 | - [All Contributors](../../contributors) 148 | 149 | ## License 150 | 151 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 152 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laimoon/model-updater", 3 | "description": "Laravel Model Updater", 4 | "keywords": [ 5 | "laimoon", 6 | "laravel-model-updater", 7 | "laravel" 8 | ], 9 | "homepage": "https://github.com/laimoon/laravel-model-updater", 10 | "license": "MIT", 11 | "type": "library", 12 | "authors": [ 13 | { 14 | "name": "Saleem Hadad", 15 | "email": "saleem@laimoon.com", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "^7.1", 21 | "illuminate/support": "5.5.*|5.6.*|5.7.*|5.8.*|6.*" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "Laimoon\\ModelUpdater\\": "src" 26 | } 27 | }, 28 | "config": { 29 | "sort-packages": true 30 | }, 31 | "extra": { 32 | "laravel": { 33 | "providers": [ 34 | "Laimoon\\ModelUpdater\\ModelUpdaterServiceProvider" 35 | ] 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Commands/MakeModelUpdater.php: -------------------------------------------------------------------------------- 1 | filesystem = $filesystem; 41 | } 42 | 43 | /** 44 | * Execute the console command. 45 | * 46 | * @return mixed 47 | */ 48 | public function handle() 49 | { 50 | $modelName = $this->argument('model').'Updater'; 51 | $updatersDir = app_path('Updaters'); 52 | $updaterPath = app_path('Updaters/').$modelName.'.php'; 53 | 54 | if (!$this->filesystem->isDirectory($updatersDir)) { 55 | $this->filesystem->makeDirectory($updatersDir, 0755, true); 56 | } 57 | 58 | if (!$this->filesystem->exists($updaterPath)) { 59 | $content = $this->getClassContentFromStub($modelName); 60 | 61 | $this->filesystem->put($updaterPath, $content); 62 | 63 | $this->info('Model updater generated'); 64 | } else { 65 | $this->error('File Already Exists!'); 66 | } 67 | } 68 | 69 | /** 70 | * @return string 71 | */ 72 | protected function getClassContentFromStub($class) 73 | { 74 | return str_replace( 75 | '{{CLASSNAME}}', 76 | $class, 77 | $this->getStubContent() 78 | ); 79 | } 80 | 81 | /** 82 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 83 | * 84 | * @return string 85 | */ 86 | protected function getStubContent() 87 | { 88 | return $this->filesystem->get(base_path('/vendor/laimoon/model-updater/stubs/ModelUpdater.stub')); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/ModelUpdaterServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 16 | $this->commands(MakeModelUpdater::class); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Traits/Updatable.php: -------------------------------------------------------------------------------- 1 | process($this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Updater.php: -------------------------------------------------------------------------------- 1 | request = $request; 35 | } 36 | 37 | /** 38 | * @param Model $model 39 | * 40 | * @return Model 41 | */ 42 | public function process(Model $model) 43 | { 44 | $this->model = $model; 45 | 46 | foreach ($this->getIntendedUpdateFields() as $field => $value) { 47 | if (method_exists($this, $field)) { 48 | $this->$field($value); 49 | } 50 | } 51 | 52 | $this->model->save(); 53 | 54 | return $this; 55 | } 56 | 57 | /** 58 | * Fetch all relevant filters from the request. 59 | * 60 | * @return array 61 | */ 62 | public function getIntendedUpdateFields() 63 | { 64 | return $this->request->only($this->fields); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /stubs/ModelUpdater.stub: -------------------------------------------------------------------------------- 1 | request->validate(['someProperty' => 'required']); 23 | // $this->model->someProperty = $value; 24 | } 25 | } 26 | --------------------------------------------------------------------------------