anourvalar / eloquent-validation
Validation feature for eloquent model (Laravel)
Installs: 9 211
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.3
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.26
- phpstan/phpstan: ^1.10
- psalm/plugin-laravel: ^2.8
- squizlabs/php_codesniffer: ^3.7
- dev-master
- 3.15.23
- 3.15.22
- 3.15.21
- 3.15.20
- 3.15.19
- 3.15.18
- 3.15.17
- 3.15.16
- 3.15.15
- 3.15.14
- 3.15.13
- 3.15.12
- 3.15.11
- 3.15.10
- 3.15.9
- 3.15.8
- 3.15.7
- 3.15.6
- 3.15.5
- 3.15.4
- 3.15.3
- 3.15.2
- 3.15.1
- 3.15.0
- 3.14.6
- 3.14.5
- 3.14.4
- 3.14.3
- 3.14.2
- 3.14.1
- 3.14.0
- 3.13.3
- 3.13.2
- 3.13.1
- 3.13.0
- 3.12.1
- 3.12.0
- 3.11.11
- 3.11.10
- 3.11.9
- 3.11.8
- 3.11.7
- 3.11.6
- 3.11.5
- 3.11.4
- 3.11.3
- 3.11.2
- 3.11.1
- 3.11.0
- 3.10.2
- 3.10.1
- 3.10.0
- 3.9.3
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.0
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.1
- 3.4.0
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.5.6
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.14.6
- 1.14.5
- 1.14.4
- 1.14.3
- 1.14.2
- 1.14.1
- 1.14.0
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
This package is auto-updated.
Last update: 2024-11-17 14:35:50 UTC
README
Installation
composer require anourvalar/eloquent-validation
Usage
Creating with validation
\App\UserPhone ::fields('user_id', 'phone_number') // fillable columns (mass assignment) ->fill(\Request::input()) ->validate() // will throw exception if it fails ->save(); }
Updating with validation
\App\UserPhone ::findOrFail(\Request::input('id')) ->fields(['user_id', 'phone_number']) // also might be an array ->fill(\Request::input()) ->validate() ->save(); }
Generate model
php artisan make:model-validation UserPhone
Model configuration
<?php namespace App; use Illuminate\Database\Eloquent\Model; class UserPhone extends Model { use \AnourValar\EloquentValidation\ModelTrait; /** * Trim columns * * @var array */ protected $trim = [ 'phone_number', // trim mutator ]; /** * '',[] => null convertation * * @var array */ protected $nullable = [ // empty string to null (convertation) mutator ]; /** * Calculated columns * * @var array */ protected $computed = [ // columns which could be changed only in listeners (observers) ]; /** * Immutable columns * * @var array */ protected $unchangeable = [ 'user_id', // unchangeable columns after creation ]; /** * Unique columns sets * * @var array */ protected $unique = [ // unique sets of columns ]; /** * Get the validation rules * * @return array */ public function saveRules() { return [ 'user_id' => ['required', 'integer'], 'phone_number' => ['required', 'string', 'min:8', 'max:13', 'unique'], ]; } /** * "Save" after-validation * * @param \Illuminate\Validation\Validator $validator * @return void */ public function saveAfterValidation(\Illuminate\Validation\Validator $validator): void { if ($this->isDirty('user_id') && !\App\User::find($this->user_id)) { $validator->errors()->add('user_id', trans('models/user_phone.user_id_not_exists')); } } /** * "Delete" after-validation * * @param \Illuminate\Validation\Validator $validator * @return void */ public function deleteAfterValidation(\Illuminate\Validation\Validator $validator): void { // } }