title: I can not insert data using ajax in Laravel, I got 500 internal server error always and i do not know why?
body: <p>I am using laravel and ajax, There is a contact form at home page and I wanna user to able to submit this form without refreshing the page, but always got this internal server error.</p>
  
  <p>I am using laravel 5.8 and jquery, I've tried solutions like using another jquery version or including the csrf or not and all is the same. </p>
  
  <p>insert function at HomeController</p>
  
  <pre><code>
  public function contactForm(Request $request) {
  
          $rules = [
              'email' =&gt; 'required|email|unique:users',
              'message' =&gt; 'required|string'
          ];
  
          $this-&gt;validate($request, $rules);
  
          if($request-&gt;fails()) {
              $failed_output = [];
              foreach ($request-&gt;getMessages()-&gt;messages() as $message) {
                  $failed_output[] = $message;
              }
          }else {
              $data = $request-&gt;except('_token');
              DB::table('user_messages')-&gt;insert($data);
  
              $success_output = "&lt;div class='alert alert-success'&gt;We got your message, thank you.&lt;/div&gt;";
  
          }
          $output = [
                  'success' =&gt; $success_output,
                  'error' =&gt; $failed_output,
              ];
  
          return response()-&gt;json([
              'success' =&gt; $success_output
          ], 200);
          // echo json_encode($output);
      }
  </code></pre>
  
  <p>my route</p>
  
  <pre><code>Route::post('/', 'HomeController@contactForm')-&gt;name('home');
  
  </code></pre>
  
  <p>html Form </p>
  
  <pre><code>&lt;form id="contactForm" autocomplete="off" method="post" action="/"&gt;
  
                              &lt;span id="form_output"&gt;&lt;/span&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;input id="email" placeholder="Your email..." value="@auth {{ auth()-&gt;user()-&gt;email }} @endauth" type="email" name="email" class="form-control" required&gt;
                                  &lt;p class="home-email-input js-error"&gt;Email is not valid&lt;/p&gt;
  
                                  @if($errors-&gt;has('email'))
                                      &lt;div class="alert alert-danger"&gt;
                                          {{ $errors-&gt;first('email') }}
                                      &lt;/div&gt;
                                  @endif
  
                              &lt;/div&gt;
                              &lt;div class="form-group"&gt;
                                  &lt;textarea id="message" placeholder="Tell us why you like this website..." name="message" class="form-control" required&gt;&lt;/textarea&gt;
                                  &lt;p class="home-message-input js-error"&gt;You message must be between 10 - 500 characters.&lt;/p&gt;
  
                                  @if($errors-&gt;has('message'))
                                      &lt;div class="alert alert-danger"&gt;
                                          {{ $errors-&gt;first('message') }}
                                      &lt;/div&gt;
                                  @endif
  
                              &lt;/div&gt;
  
                              &lt;input type="submit" class="btn btn-primary float-right" value="Send"&gt;
  
                          &lt;/form&gt;
  
  </code></pre>
  
  <p>javascript code (ajax call)</p>
  
  <pre><code>$.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
  
      $("#contactForm").submit(function(evt) {
  
          evt.preventDefault();
  
          let email = $("#contactForm div #email").val();
          let msg = $("#contactForm div #message").val();
  
          if(email.length &lt; 10) {
              $("#contactForm div p.home-email-input").css("display", "block");
              return false;
          }
          if(msg.length &lt; 10 || msg.length &gt; 500) {
              $("#contactForm div p.home-message-input").css("display", "block");
              return false;
          }
  
          // Insert By Ajax
  
          let form_data = $(this).serialize();
          console.log(form_data);
          $.ajax({
              method: 'POST',
              url: "/",
              headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
              },
              data: form_data,
              dataType: 'json',
              success: function(data) {
                  if(data.error.length &gt; 0) {
                      let errors = "";
                      for(let i = 0; i &lt; data.error.length; i++) {
                          errors += "&lt;div class='alert alert-danger'&gt;"+data.error[i]+"&lt;/div&gt;";
                      }
                      $("#form_output").html($errors);
                  }else {
  
                      $("#form_output").html(data.success);
                      $("#contactForm")[0].reset();
                  }
              },
  
          });
  
      });
  </code></pre>
  
  <p>I wanna the result to be a little success message above the input but I got this error :</p>
  
  <p>POST <a href="http://127.0.0.1:8000/" rel="nofollow noreferrer">http://127.0.0.1:8000/</a> 500 (Internal Server Error)</p>