anourvalar/eloquent-validation
Validation feature for eloquent model (Laravel)
12,892
| Install | |
|---|---|
composer require anourvalar/eloquent-validation |
|
| Latest Version: | 3.18.1 |
| PHP: | ^8.3 |
| License: | MIT |
| Last Updated: | Aug 7, 2026 |
| Links: | GitHub · Packagist |
Maintainer: AnourValar
Validation for Eloquent Models
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
{
//
}
/**
* "Restore" after-validation
*
* @param \Illuminate\Validation\Validator $validator
* @return void
*/
public function restoreAfterValidation(\Illuminate\Validation\Validator $validator): void
{
//
}
}
Related Packages
theriddleofenigma/laravel-model-validation
Effortless, self-contained validation for your Eloquent models.
12,632
33
innoscience/eloquental
A rather clever model for Laravel's Eloquent ORM with self-validation, ordering,...
384
3
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| 3.18.1 | ^8.3 | ^8.0| | MIT | |
| 3.18.0 | ^8.3 | ^8.0| | MIT | |
| 3.17.7 | ^8.3 | ^8.0| | MIT | |
| 3.17.6 | ^8.3 | ^8.0| | MIT | |
| 3.17.5 | ^8.3 | ^8.0| | MIT | |
| 3.17.4 | ^8.3 | ^8.0| | MIT | |
| 3.17.3 | ^8.3 | ^8.0| | MIT | |
| 3.17.2 | ^8.3 | ^8.0| | MIT | |
| 3.17.1 | ^8.3 | ^8.0| | MIT | |
| 3.17.0 | ^8.3 | ^8.0| | MIT | |
| 3.16.14 | ^8.3 | ^8.0| | MIT | |
| 3.16.13 | ^8.3 | ^8.0| | MIT | |
| 3.16.12 | ^8.3 | ^8.0| | MIT | |
| 3.16.11 | ^8.3 | ^8.0| | MIT | |
| 3.16.10 | ^8.3 | ^8.0| | MIT | |
| 3.16.9 | ^8.3 | ^8.0| | MIT | |
| 3.16.8 | ^8.3 | ^8.0| | MIT | |
| 3.16.7 | ^8.3 | ^8.0| | MIT | |
| 3.16.6 | ^8.3 | ^8.0| | MIT | |
| 3.16.5 | ^8.3 | ^8.0| | MIT | |
| 3.16.4 | ^8.3 | ^8.0| | MIT | |
| 3.16.3 | ^8.3 | ^8.0| | MIT | |
| 3.16.2 | ^8.3 | ^8.0| | MIT | |
| 3.16.1 | ^8.3 | ^8.0| | MIT | |
| 3.16.0 | ^8.3 | ^8.0| | MIT | |
| 3.15.38 | ^8.3 | ^8.0| | MIT | |
| 3.15.37 | ^8.3 | ^8.0| | MIT | |
| 3.15.36 | ^8.3 | ^8.0| | MIT | |
| 3.15.35 | ^8.3 | ^8.0| | MIT | |
| 3.15.34 | ^8.3 | ^8.0| | MIT |
Showing the latest 30 of 163. See every release on Packagist