How to check request is Ajax or not in Laravel?
Sometimes, we need to check request is ajax or not in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10. If you want to call same method but if request is ajax then you perform different, then you can do by using $request object.
In Laravel 5 $request object provide method to check request is ajax or not, In following example you can see how it's works.
You can check directly from Request facade and also from request object, both are same, so let's see bellow example.
Example 1:
public function index(Request $request)
{
if($request->ajax()){
return response()->json(['ajax']);
}
return response()->json(['http']);
}
Example 2:
public function index()
{
if(Request::ajax()){
return response()->json(['ajax']);
}
return response()->json(['http']);
}
I hope it can help you...

Hardik Savani
I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.