How to Check Request is Ajax or Not in Laravel?
Sep 23, 2019
Hi Guys,
This extensive guide will teach you how to check ajax request in laravel. It's a simple example of laravel check request is ajax. We will use laravel check if request is ajax. If you have a question about how to check if request is ajax or not in laravel then I will give a simple example with a solution.
Sometime you need to check if request is ajax then response data only and if request is not ajax then response complete view in your application.
By using ajax() method in Laravel, you can check request is ajax or not.
Laravel Request class has many method to read HTTP request for the current request. You can also check if request is over https or request has json content type.
You can use directly Request facade that grant you access to the current request or you can use instance of Request Class.
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']);
}
Example 3 :
if (Request::secure())
{
//
}
It will help you....
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial