how to make ajax call in laravel

0

Hello, everyone in this post we are share with you how to make simple ajax crud inlaravel application from the scratch.

You are going to work with ajax data post to controller or route in laravel 5. There are some need to get ajax call work correctly. Your requirement is csrf token in meta tag.

<meta name="csrf-token" content="{{ csrf_token() }}" />

Ajax data post to controller or route in laravel 5.

$('.ViewUserDetail').click(function(){
    var userid = $(this).attr('id');
    var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

     $.ajax({
           type:'POST',
           url:"{{url('view-user-detail')}}",
           data: {_token: CSRF_TOKEN,userid:userid},
           dataType: 'JSON',
           success:function(data){
              $('#ViewModalChallan').html(data.success);
               $('#ViewModalChallan').modal('show');
           }
    });
});

Ajax to route call

 Route::post('view-user-detail', [
        'as' => 'view-user-detail',
        'uses' => 'AddUserController@ViewUserDetail'
]);

Now finally controller call

public function ViewUserDetail(Request $request)
{
   $userid = $request->userid;   
}