moraesgil / entity-validator-trait
A simple Laravel trait for call Validation by your entitie to allow data creating or updating.
Installs: 283
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/moraesgil/entity-validator-trait
This package is auto-updated.
Last update: 2025-10-28 10:13:55 UTC
README
A simple Laravel trait for call Validation by your entities to allow data creating or updating.
Installation
With Composer
$ composer require moraesgil/entity-validator-trait
{
   "require": {
      "moraesgil/entity-validator-trait": "~1.0"
   }
}
Sample
... //Your Model use Traits\Entities\EntityValidatorTrait; //<<<<< add this line class YourLaravelEntity extends Model { use EntityValidatorTrait; protected $table = 'samples'; protected $rules = [ //<<<<< add this array with validation 'id' => 'required|max:80|unique:samples,id,@except,id', 'label' => 'required|max:80|unique:samples,label,@except,label',//<<<<< @except will be replaced by id value ]; }
Now you can call validate in your controller before store or update
// Your Controller class SampleController extends Controller { public function store(Request $request) { $model = new YourLaravelEntity(); //here is the magic if ($model->hasRules()) { //<<<<< check has rules if (!$model->validate($request->all())) { //<<<<< validate return response()->json($model->errors, 400); } } return response()->json($model->create($request->all()), 201); } public function update(Request $request) { //here is the magic if ($model->hasRules()) { //<<<<< check has rules if (!$model->validate($request->all(), $id)) { //<<<<< validate return response()->json($model->errors, 400); } } return response()->json($model->update($request->all()), 201); } }
Authors
- Gilberto PrudĂȘncio Vaz de Moraes - Initial work - MoraesGil
 
License
This project is licensed under the MIT License - see the LICENSE file for details