title: How to check if validation fail when using form-request in Laravel?
body: <p>I am trying to write a CRUD for an API. However, when the validation fail, instead of redirecting the user to the home page, I want to return <code>json</code> based response with the errors.</p>
  
  <p>I am able to do that using the following code</p>
  
  <pre><code>public function store(Request $request)
  {
      try {
          $validator = $this-&gt;getValidator($request);
  
          if ($validator-&gt;fails()) {
              return $this-&gt;errorResponse($validator-&gt;errors()-&gt;all());
          }
  
          $asset = Asset::create($request-&gt;all());
  
          return $this-&gt;successResponse(
              'Asset was successfully added!',
              $this-&gt;transform($asset)
          );
      } catch (Exception $exception) {
          return $this-&gt;errorResponse('Unexpected error occurred while trying to process your request!');
      }
  }
  
  /**
   * Gets a new validator instance with the defined rules.
   *
   * @param Illuminate\Http\Request $request
   *
   * @return Illuminate\Support\Facades\Validator
   */
  protected function getValidator(Request $request)
  {
      $rules = [
          'name' =&gt; 'required|string|min:1|max:255',
          'category_id' =&gt; 'required',
          'cost' =&gt; 'required|numeric|min:-9999999.999|max:9999999.999',
          'purchased_at' =&gt; 'nullable|string|min:0|max:255',
          'notes' =&gt; 'nullable|string|min:0|max:1000',
      ];
  
      return Validator::make($request-&gt;all(), $rules);
  }
  </code></pre>
  
  <p>Now, I like to extract some of my code into a <code>form-request</code> to clean up my controller little more. I like to change my code to something like the code below. </p>
  
  <pre><code>public function store(AssetsFormRequest $request)
  {
      try {
          if ($request-&gt;fails()) {
              return $this-&gt;errorResponse($request-&gt;errors()-&gt;all());
          }            
          $asset = Asset::create($request-&gt;all());
  
          return $this-&gt;successResponse(
              'Asset was successfully added!',
              $this-&gt;transform($asset)
          );
      } catch (Exception $exception) {
          return $this-&gt;errorResponse('Unexpected error occurred while trying to process your request!');
      }
  }
  </code></pre>
  
  <p>As you can probably tell that <code>$request-&gt;fails()</code> and  <code>$request-&gt;errors()-&gt;all()</code> is not going to work. How can I check if the request failed and then how can I get the errors out of the form-request?</p>
  
  <p>For your reference, here is how my <code>AssetsFormRequest</code> class look like</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Requests;
  
  use Illuminate\Foundation\Http\FormRequest;
  
  class AssetsFormRequest extends FormRequest
  {
      /**
       * Determine if the user is authorized to make this request.
       *
       * @return bool
       */
      public function authorize()
      {
          return true;
      }
  
      /**
       * Get the validation rules that apply to the request.
       *
       * @return array
       */
      public function rules()
      {
          return [
              'name' =&gt; 'required|string|min:1|max:255',
              'category_id' =&gt; 'required',
              'cost' =&gt; 'required|numeric|min:-9999999.999|max:9999999.999',
              'purchased_at' =&gt; 'nullable|string|min:0|max:255',
              'notes' =&gt; 'nullable|string|min:0|max:1000',
          ];
      }
  }
  </code></pre>