seantrant
12/21/2016 - 4:23 PM

Ajax toggle in laravel

Ajax toggle in laravel

HTML:
<form action="">
	<label for='featured' >Featured:</lable>
  <input type="checkbox" class = 'featured' id='{{$user->id}}' name="featured" value="{{ $user->userEmployerData->admin_allow_promote }}" @if($user->userEmployerData->admin_allow_promote == 1) checked=checked @endif>
</form>
												
Script:
<script>
	$(document).ready(function(){
		$(".featured").change(function() {
			var user_id = this.id
			$.ajax({
							type:'GET',
							url:'/admin/feature-business/'+user_id,
							headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}' },
							data: { "user_id" : user_id },
							success: function(data){
								if(data.data.success){
									// alert('sucess');
								}
							}
					});
			});
	});
</script>

Route:
Route::get('/admin/feature-business/{id}', 'Backend\AdminPagesController@featureBusiness');

Controller:
    public function featureBusiness($id, Request $request)
    {
      // dd('got here');
      $user = UserEmployer::findOrFail($id);
      if($user->admin_allow_promote == 1){
          $user->admin_allow_promote = 0;
      } else {
          $user->admin_allow_promote = 1;
      }
      return response()->json([
        'data' => [
          'success' => $user->save(),
        ]
      ]);
    }