Laravel check if ajax request or not example


In this article, i will share with you how to check request is ajax on not in laravel 5 application.

We maybe need to determine if request is ajax then response should be in json and id using http request then return view. So at that time need to determine request. In this example you will lean how to check if request is ajax or not.

Laravel provide ajax() method of Request Facade to identify request is ajax or not. You can use both way as listed bellow:

$request->ajax()

Request::ajax()

But actually, Both things are same to check request is ajax or not. So, let's see bellow example, how you can do it using controller method.

Example:

public function myIndex(Request $request)

{

if($request->ajax()){

return response()->json(['status'=>'Request is Ajax']);

}

return response()->json(['status'=>'Request is Http']);

}

public function myIndex(Request $request)

{

if(Request::ajax()){

return response()->json(['status'=>'Request is Ajax']);

}

return response()->json(['status'=>'Request is Http']);

}

You can simply check by as above both example....Enjoy....


May 8, 2017 | 360 Viewer | Category : Laravel Ajax
Related Posts:



Blog Search