Laravel has a validated function which runs your defined rules and automatically sends errors to your view. This is awesome for most projects but sometimes you may want to handle / read these errors yourself. Or maybe just do it before sending them to view. Here is how you can do that.
- First of all, add validator at top of your fileuse Illuminate\Support\Facades\Validator;
- Then create validator with your rules like this
$validator = Validator::make($requestData, [
      'user_id' => ['required', 'integer']
    ]);
- Then use following snippet to get errors as array
if ($validator->fails()) {
      $fieldsWithErrors = $validator->messages()->get('*');
      // do anything here
    }