insidert

Send custom JSON error response in Laravel

Comes handy when dealing with ajax requests.

First published on

Instead of using a validator, you can send custom JSON error responses in Laravel. If you are using axios library, the catch() method will grab the response.

if ($some_check_here) {
  return response()->json([
    'errors' => [
      'contact_type' => [
         'You must give either valid email or phone number'
      ]
    ]
  ], 422);
}

#laravel
#code