How to check request is ajax or not in laravel6

  1 year ago     Views:  566  
Site Admin
4 Follows


In this tutorials we will share with you how to check request is ajax or not in laravel. some time we work with laravel API then it is help more. because help of this you can manage you API ajax request and web request in same controller so, you can manage you code very easy way.

You can check ajax request help of ajax() function in laravel.

Laravel Request class has many method to read HTTP request from the currenct request. ajax() one of them

Example - 1

public function index(Request $request)
{
    if($request->ajax()){
        return response()->json(['status'=>'Ajax request']);
    }
    return response()->json(['status'=>'Http request']);
}

Example - 2

public function index()
{
    if($request->ajax()){
        return response()->json(['status'=>'Ajax request']);
    }
    return response()->json(['status'=>'Http request']);
}

We hope these small tutorials help everyone. if you like this article then please comment below. Thanks..


Was this article helpful?

0 out of 0 person found this article helpful.

Leave a comment

OR

No Comments