title: Laravel 5: Ajax Post 500 (Internal Server Error) Error
body: <h2>I'm trying to submit data via ajax (add to cart) but got an error in console  POST <a href="http://localhost/glutax9gs/cartupdate" rel="nofollow noreferrer">http://localhost/glutax9gs/cartupdate</a> 500 (Internal Server Error)</h2>
  
  <p>What's wrong with my code? Is it the javascript or the controller?</p>
  
  <hr>
  
  <p>here is my Route:</p>
  
  <pre class="lang-php prettyprint-override"><code>Route::get('/cartupdate', 'FrontEndController@cartupdate')-&gt;name('update.cart');
  Route::post('/cartupdate', 'FrontEndController@cartupdate')-&gt;name('cart.update');
  </code></pre>
  
  <p>Controller code is here:</p>
  
  <pre class="lang-php prettyprint-override"><code>    public function cartupdate(Request $request)
      {
  
          if ($request-&gt;isMethod('post')){
  
              if (empty(Session::get('uniqueid'))){
  
                  $cart = new Cart;
                  $cart-&gt;fill($request-&gt;all());
                  Session::put('uniqueid', $request-&gt;uniqueid);
                  $cart-&gt;save();
  
              }else{
  
                  $cart = Cart::where('uniqueid',$request-&gt;uniqueid)
                      -&gt;where('product',$request-&gt;product)-&gt;first();
                  //$carts = Cart::where('uniqueid',$request-&gt;uniqueid)
                          //-&gt;where('product',$request-&gt;product)-&gt;count();
                  if (count($cart) &gt; 0 ){
                      $data =  $request-&gt;all();
                      $cart-&gt;update($data);
                  }else{
                      $cart = new Cart;
                      $cart-&gt;fill($request-&gt;all());
                      $cart-&gt;save();
                  }
  
              }
              return response()-&gt;json(['response' =&gt; 'Successfully Added to Cart.','product' =&gt; $request-&gt;product]);
          }
  
          $getcart = Cart::where('uniqueid',Session::get('uniqueid'))-&gt;get();
  
          return response()-&gt;json(['response' =&gt; $getcart]);
      }
  </code></pre>
  
  <p>jQuery code is here:</p>
  
  <pre><code>    $(".to-cart").click(function(){
  
          var formData = $(this).parents('form:first').serializeArray();
          $.ajax({
              type: "POST",
              url: '{{route("cart.update")}}',
              data:formData,
              success: function (data) {
                  getCart();
                  $.notify(data.response, "success");
              },
              error: function (data) {
                  console.log('Error:', data);
              }
          });
      });
  </code></pre>
  
  <p>View code is here:</p>
  
  <pre class="lang-php prettyprint-override"><code>        &lt;form&gt;
              &lt;p&gt;
                  {{csrf_field()}}
                  @if(Session::has('uniqueid'))
                      &lt;input type="hidden" name="uniqueid" value="{{Session::get('uniqueid')}}"&gt;
                  @else
                      &lt;input type="hidden" name="uniqueid" value="{{str_random(7)}}"&gt;
                  @endif
        &lt;input type="hidden" name="title" value="{{$product-&gt;title}}"&gt;
        &lt;input type="hidden" name="product" value="{{$product-&gt;id}}"&gt;
        &lt;input type="hidden" id="cost" name="cost" value="{{$product-&gt;price}}"&gt;
        &lt;input type="hidden" id="quantity" name="quantity" value="1"&gt;
             @if($product-&gt;stock != 0 || $product-&gt;stock === null )
      &lt;button type="button" class="button style-10 to-cart"&gt;Add to cart&lt;/button&gt;
                @else
             &lt;button type="button" class="button style-10 to-cart" disabled&gt;Out Of Stock&lt;/button&gt;
             @endif
              {{--&lt;button type="button" class="button style-10 hidden-sm to-cart"&gt;&lt;i class="fa fa-shopping-cart"&gt;&lt;/i&gt;Add to cart&lt;/button&gt;--}}
              &lt;/p&gt;
          &lt;/form&gt;
  </code></pre>