title: Laravel Update Database with ajax
body: <p>Hey guys I'm using laravel 5.7 and I'm attempting to make a ajax post request to update my database. The ajax would post based on a checkbox on change function. Example if i toggle off the checkbox it would send a request and update my status to <code>Inactive</code> in my <code>User</code> table. After attempting it, i had an error of <code>405 (Method Not Allowed)</code>. Anyone able to note what am i doing wrong? Sorry if there are some wrong codes or syntax in my codes as I'm very new to Ajax. Any help would be appreciated.</p>
  
  <p><strong>Ajax</strong></p>
  
  <pre><code>$(document).ready(function(){
      $.ajax({
          type:'get',
          url:'{!!URL::to('findStatus')!!}',
          success:function(data){
              for(var i=0;i&lt;data.length;i++){
  
                  var checkBox = document.getElementById('switch-'+data[i].u_id);
  
                  console.log(checkBox);
  
                  if(data[i].status == "Active"){
                      $('#switch-'+data[i].u_id).prop('checked',true);
                  }
  
                  else if(data[i].status == "Inactive")
                  {
                      $('#switch-'+data[i].u_id).prop('checked',false);
                  } 
  
  
                  $('#switch-'+data[i].u_id).change(function(){
                      $.ajax({
                          type: "POST",
                          url : '{!!URL::to('admin/{admin}')!!}',
                          success:function(data){
                              console.log(data);
                          }
                      });
                  });
              }
          },
          error:function(data){
              console.log('ERROR');
          }
      });
  });
  </code></pre>
  
  <p><strong>Route</strong></p>
  
  <pre><code>Route::resource('admin','AdminController');  &lt;&lt; I'm using the update method from the resource controller
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function update(Request $request, $id)
  {
      $user = User::find($id);
      if($user-&gt;status == "Active"){
          $user-&gt;status = "Inactive";
          $user-&gt;save();
      }else{
          $user-&gt;status = "Active";
          $user-&gt;save();
      }
      return response()-&gt;json($user);
  }
  </code></pre>
  
  <p><strong>Form</strong></p>
  
  <pre><code>{!!Form::open(array('action'=&gt;['AdminController@update',$item-&gt;u_id],'method'=&gt;'POST','id'=&gt;'update'))!!}
      &lt;input type="hidden" name="_token" value="{{ csrf_token() }}"&gt;
      &lt;input type="hidden" name="u_id" id="u_id" value="{{$item-&gt;u_id}}"&gt;
      &lt;label class="custom-control custom-checkbox"&gt;
          &lt;input type="checkbox" id="switch-{{$item-&gt;u_id}}" class="custom-control-input"&gt;
          &lt;span class="custom-control-indicator"&gt;&lt;/span&gt;
      &lt;/label&gt;
      {{-- &lt;button class="btn btn-primary"&gt;Update&lt;/button&gt; --}}
  {{Form::hidden('_method','PUT')}}
  {!!Form::close()!!}
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have managed to "pass" the u_id to my post request by getting the id through <code>target.id</code> and splitting it with <code>-</code>. It is not the most elegant way but it works. But now im getting an error </p>
  
  <blockquote>
    <p>POST <a href="http://manageme.test/admin/%7B2%7D" rel="nofollow noreferrer">http://manageme.test/admin/%7B2%7D</a> 500 (Internal Server Error)</p>
  </blockquote>
  
  <p>Here is what i have updated in my codes.</p>
  
  <pre><code>$('#switch-'+data[i].u_id).change(function(e){
  
      console.log(e.target.id);
  
      var s = e.target.id;
      var split = s.split('-')[1];
  
      $.ajax({
          type: "POST",
          url: `{!!url('admin/')!!}/{${split}}`,
          data: { _token: "{{ csrf_token() }}", _method: "PUT" },
          success:function(data){
              console.log(data);
          }
      }); 
  });
  </code></pre>
  
  <p>these are inside my update controller </p>
  
  <pre><code>public function update(Request $request, $id)
  {
      $user = User::find($id);
      if($user-&gt;status == "Active"){
          $user-&gt;status = "Inactive";
          $user-&gt;save();
      }else{
          $user-&gt;status = "Active";
          $user-&gt;save();
      }
      return response()-&gt;json($user);
  }
  </code></pre>
  
  <p>I have also looked at the error inside the network tab of the dev tools the error message from laravel is <code>message: "Trying to get property 'status' of non-object"</code>. I think it cant find any <code>$user</code> inside the update method</p>