title: How i can return a customized response in a FormRequest class in Laravel 5.5?
body: <p>I am making an API and i want return the array of errors with a format as the one that <code>$validator-&gt;errors();</code> generates when i validate the request by the manual way. But i cant manipulate the response. I wanna find thhe correct way to make it.</p>
  
  <p>This could be done in Laravel 5.4 with the <code>formatErrors</code> method and including the <code>Illuminate\Contracts\Validation\Validator</code> class in the FormRequest class but for version 5.5 it does not work. I do not know how to do it.</p>
  
  <p><strong>This is my Controller:</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Http\Requests\ProductRequest;
  use Illuminate\Validation\Rule;
  use App\Product;
  
  class ProductController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index(ProductRequest $request)
      {
          $products = Product::where('code', 'LIKE', '%'.$request-&gt;input('search').'%')
          -&gt;where('name', 'LIKE', '%'.$request-&gt;input('search').'%')
          -&gt;paginate(10);
          $products-&gt;withPath($request-&gt;fullUrl());
          return $products;
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
  
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(ProductRequest $request)
      {
          $product = new Product($request-&gt;validated());
          $product-&gt;save();
          return response('', 201);
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
          $product = Product::find($id);
          return response($product, 200);
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          //
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(ProductRequest $request, $id)
      {
          $product = Product::find($id);
          $product-&gt;fill($request-&gt;validated());
          $product-&gt;save();
          return response('', 200);
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id)
      {
          $product = Product::find($id);
          $product-&gt;delete();
          return response('', 204);
      }
  }
  </code></pre>
  
  <p><strong>This is mi FormRequest class</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Requests;
  
  use Illuminate\Foundation\Http\FormRequest;
  use Illuminate\Validation\Rule;
  
  class ProductRequest 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()
      {
          switch($this-&gt;method())
          {
              case 'GET':
              {
                  return [
                      'code' =&gt; 'string',
                      'name' =&gt; 'string',
                  ];
              } break;
              case 'POST':
              {
                  return [
                      'code' =&gt; 'required|unique:Products,code',
                      'name' =&gt; 'required',
                      'description' =&gt; 'required',
                      'quantity' =&gt; 'required|min:0',
                      'price' =&gt; 'required|numeric',
                      'extemp' =&gt; [
                          Rule::in(['tax', 'free']),
                      ]
                  ];
              } break;
              case 'PUT':
              {
                  return [
                      'code' =&gt; 'unique:products,code,'.$this-&gt;route('product'),
                      'name' =&gt; 'string:min:1',
                      'description' =&gt; 'string|min:1',
                      'quantity' =&gt; 'integer|min:0',
                      'price' =&gt; 'numeric',
                      'extemp' =&gt; [
                          Rule::in(['tax', 'free']),
                      ],
                  ];
              } break;
              case 'PATCH': break;
              case 'DELETE': break;
              default:
              {
                  return [];
              } break;
          }
      }
  
      /**
       * Get the error messages for the defined validation rules.
       *
       * @return array
       */
      public function messages()
      {
          return [
              //Product
              'code.required' =&gt; 'El :attribute es obligatorio.',
              'code.unique' =&gt; 'El :attribute ya se encuentra registrado.',
              'name.required' =&gt; 'El :attribute es obligatorio.',
              'name.min' =&gt; 'El :attribute es obligatorio.',
              'description.required' =&gt; 'El :attribute es obligatorio.',
              'description.min' =&gt; 'El :attribute es obligatorio.',
              'quantity.required' =&gt; 'La :attribute es obligatoria.',
              'quantity.integer' =&gt; 'La :attribute debe ser un número entero.',
              'quantity.min' =&gt; 'La :attribute debe ser al menos :min.',
              'price.required' =&gt; 'El :attribute es obligatorio.',
              'price.numeric' =&gt; 'El :attribute debe ser un valor numérico.',
              'extemp.in' =&gt; 'El :attribute seleccionado es inválido.',
          ];
      }
      public function attributes(){
          return [
              'code' =&gt; 'código',
              'name' =&gt; 'nombre',
              'description' =&gt; 'descripción',
              'quantity' =&gt; 'cantidad',
              'price' =&gt; 'precio',
              'extemp' =&gt; 'exento',
          ];
      }
  }
  </code></pre>
  
  <p><strong>The Response what i have:</strong></p>
  
  <pre><code>{
      "message": "The given data was invalid.",
      "errors": {
          "code": [
              "El código es obligatorio."
          ],
          "name": [
              "El nombre es obligatorio."
          ],
          "description": [
              "El descripción es obligatorio."
          ],
          "quantity": [
              "La cantidad es obligatoria."
          ],
          "price": [
              "El precio es obligatorio."
          ]
      }
  }
  </code></pre>
  
  <p><strong>The Response what i want</strong> (with <code>$validator-&gt;errors();</code>)</p>
  
  <pre><code>[
      "El código es obligatorio.",
      "El nombre es obligatorio.",
      "El descripción es obligatorio.",
      "La cantidad es obligatoria.",
      "El precio es obligatorio."
  ]
  </code></pre>