title: Get Laravel to return JSON
body: <p>Hi am struggling to get my validated error messages in JSON. I don't know why it is so hard to do this in laravel. I am trying to use a ajax request to get back a HTTP response with my form data errors. Here is my request code </p>
  
  <pre><code>&lt;?php
  
      namespace App\Http\Requests;
  
      use App\Http\Requests\Request;
  
      use Auth;
  
      use Log;
  
  
      class PostRequest extends Request
      {
          /**
           * 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 wantsJson()
          {
              return true;
          }
  
      public function response(array $errors) 
         {  
  
              if ($this-&gt;ajax())
             {
                  return new JsonResponse($errors, 422);
             }
             return $this-&gt;redirector-&gt;to($this-&gt;getRedirectUrl())
                                          -&gt;withInput($this-&gt;except($this-&gt;dontFlash))
                                          -&gt;withErrors($errors);
      }
  
      public function rules() {
  
        $rules = [];
  
        foreach($this-&gt;request-&gt;get('address') as $key =&gt; $val) {
  
          $rules['address.'.$key] = 'required';
  
        }
  
  
       foreach($this-&gt;request-&gt;get('city') as $key =&gt; $val) {
  
          $rules['city.'.$key] = 'required';
  
        }
    return $rules;
     }
  
  
   public function messages() {
  
  
    foreach($this-&gt;request-&gt;get('address') as $key =&gt; $val) {
  
  
      $messages['address.'.$key.'.required']="*Please fill in the address";
  
            } 
  
          foreach($this-&gt;request-&gt;get('city') as $key =&gt; $val) {
  
  
               $messages['city.'.$key.'.required']="*Please fill in the city";
  
           } 
            return  $messages;
  
            }
  
                  }
  </code></pre>
  
  <p>Here is my controller code </p>
  
  <p>
  
  <pre><code>  namespace App\Http\Controllers;
  
    use Illuminate\Http\Request;
  
    use App\Http\Requests;
  
    use App\Http\Requests\PostRequest;
  
    use Auth;
  
    class propContoller extends Controller {
  
  
       public function __construct() {
  
             $this-&gt;middleware('auth');
       }
  
  
       public function main ()  {
  
               $amount = Auth::user()-&gt;units;   // pull amount of units from data base and create forms 
  
             return view("myprop",compact("amount"));
       }
  
       public function Postdata(PostRequest $request) {
  
  
  
  
  
        }
    }
  </code></pre>
  
  <p>and my ajax request </p>
  
  <pre><code>$("#from").submit(function(event) {
  
  $.ajax({
    type: 'post',
    url: '/myproperties/save',
    data: { '_token': token, 'data': "good" },
    dataType: 'json',
    success: function(data){
    console.log(data);
    },
    error: function(data){
      var errors = data.responseJSON;
      console.log(errors);
      // Render the errors with js ...
    }
  
  });
  </code></pre>
  
  <p>and my HTML</p>
  
  <pre><code>       &lt;div  class="form-group col-md-6"&gt;
  
  
                  &lt;div  class= "col-md-6  form-group {{ $errors-&gt;has('address.'.$i) ? ' has-error' : '' }}"&gt;
  
              &lt;label for={{$id = "Address".$i}}&gt;Address&lt;/label&gt;
                &lt;input  value="{{ old('address.'.$i) }}" type="text"  name="address[{{$i}}]" class="form-control" id={{$id = "Address".$i}} placeholder="Street Address"&gt;
  
  
                @if ($errors-&gt;has('address.'.$i))
                            &lt;span class="help-block"&gt;
                            &lt;strong&gt;{{ $errors-&gt;first('address.'.$i) }}&lt;/strong&gt;
                                &lt;/span&gt;
                   @endif
  
                  &lt;/div&gt;
  
  &lt;div  class="col-md-6 form-group {{ $errors-&gt;has('city.'.$i) ? ' has-error' : '' }}"&gt;
  
             &lt;label for={{$id = "city".$i}}&gt;City&lt;/label&gt;
              &lt;input value="{{ old('city.'.$i) }}" type="text"  name="city[{{$i}}]"  class="form-control" id={{$id = "City".$i}} placeholder="City"&gt;
  
               @if ($errors-&gt;has('city.'.$i))
                         &lt;span class="help-block"&gt;
                             &lt;strong&gt;{{ $errors-&gt;first('city.'.$i) }}&lt;/strong&gt;
                              &lt;/span&gt;
                          @endif
                    &lt;/div&gt;
  </code></pre>
  
  <p>Some one please help I am  noob to laravel so go easy on me </p>
  
  <p>Thank you ! </p>