title: Laravel - 500 (Internal Server Error) on Ajax Call
body: <p>I've been trying on this tutorial (<a href="https://www.youtube.com/watch?time_continue=7&amp;v=D4ny-CboZC0" rel="nofollow noreferrer">https://www.youtube.com/watch?time_continue=7&amp;v=D4ny-CboZC0</a>) with respect to my context and I'm getting this error:</p>
  
  <blockquote>
    <p>POST <a href="http://127.0.0.1:8000/transactions/fetch" rel="nofollow noreferrer">http://127.0.0.1:8000/transactions/fetch</a> 500 (Internal Server Error)</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/qd0lj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qd0lj.png" alt="enter image description here"></a></p>
  
  <p><strong>create.blade.php</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>@extends('layouts.app')
  @section('title', 'New Transaction')
  @section('content')
  &lt;form action="/transactions" method="POST"&gt;
      &lt;div class="row"&gt;
          &lt;label&gt;Item Name&lt;/label&gt;
          &lt;input type="text" name="item_name" id="item_name" class="form-control input-lg" placeholder="Item Name"&gt;
          &lt;div id="itemList"&gt;&lt;/div&gt;
          {{ csrf_field()}}
          &lt;label&gt;Quantity&lt;/label&gt;
          &lt;input type="text" name="quantity[]"  value="" placeholder="Quantity"&gt;
          {{ $errors-&gt;first('quantity')}}
      &lt;/div&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;Complete Order&lt;/button&gt;
      @csrf
  &lt;/form&gt;
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function(){
      $('#item_name').keyup(function(){
          var query = $(this).val();
          if(query!=''){
              var _token = $('input[name="_token"]').val();
              console.log(_token);
              $.ajax({
                  url: "{{route('autocomplete.fetch')}}",
                  method: "POST",
                  data: {query: query, _token:_token},
                  success:function(data){
                      $('#itemList').fadeIn();
                      $('#itemList').html(data);
                  }
              })
          }
      });
  })
  &lt;/script&gt;
  @endsection
  </code></pre>
  
  <p>fetch function in <strong>TransactionController.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function fetch(Request $request)
  {
      if($request-&gt;get('query')) {
          $query= $request-&gt;get('query');
          $data = DB::table('items')
          -&gt;where('item_name', 'LIKE', '%{$query}%')
          -&gt;get();
      }
  
      $output = '&lt;ul class="dropdown-menu" style="display:block; position:relative"&gt;';
      foreach ($data as $row) {
          $output.= '&lt;li&gt; &lt;a href="#"&gt;' .$row-&gt;item_name.'&lt;/a&gt;&lt;/li&gt;';
      }
      $output .= '&lt;/ul&gt;';
      echo $output;
  }
  </code></pre>
  
  <p><strong>web.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>Route::resource('transactions', 'TransactionController');
  Route::post('/transactions/fetch', 'TransactionController@fetch')-&gt;name('autocomplete.fetch');
  </code></pre>
  
  <p>Kindly help me find out where exactly the problem is. I tried many methods seen online but none worked.</p>
  
  <p>Thanks in advance!</p>