You can also look at checking in your model and throwing a ValidationException that will be handled as usual in your controller (with an error package, etc.). For instance:
abstract class BaseModel extends Model implements ModelInterface {
protected $validationRules = [];
public function validate($rules=[]) {
if (empty($rules))
$rules = $this->toArray();
$validator = \Validator::make($rules,$this->validationRules);
if ($validator->fails())
throw new ValidationException($validator);
}
public function validateAndFill($inputArray) {
$this->validate($inputArray);
$this->fill($inputArray);
}
}
Then in my controller:
public function store(Request $request) {
$person = $this->personService->create($request->input());
return redirect()->route('people.index', $person)->with('status', $person->first_name.' has been saved');
}
Finally in my base class of service
abstract class BaseResourceService {
protected $dataService;
protected $modelClassName;
public function create($inputArray) {
try {
$arr = $inputArray;
$object = new $this->modelClassName();
$object->validateAndFill($arr);
$this->dataService->create($object);
return $object;
}
catch (Exception $exception) {
$this->handleError($exception);
}
}
, . , -/ .
, , $person-> validate() , , .