How to check request is ajax or not in Laravel

  435 views   6 months ago Laravel

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..

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]