How to check request is Ajax or not in Laravel 5?

How to check request is Ajax or not in Laravel 5?

In this post we will give you information about How to check request is Ajax or not in Laravel 5?. Hear we will give you detail about How to check request is Ajax or not in Laravel 5?And how to use it also give you demo for it if it is necessary.

In this post, I will let you know how to determine request is ajax or not in Laravel 5.

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 functionindex(Request $request)
  2. {
  3. if($request->ajax()){
  4. returnresponse()->json(['status'=>'Ajax request']);
  5. }
  6. returnresponse()->json(['status'=>'Http request']);
  7. }
public function index(Request $request)
{
    if($request->ajax()){
        return response()->json(['status'=>'Ajax request']);
    }
    return response()->json(['status'=>'Http request']);
}

  1. public functionindex()
  2. {
  3. if(Request::ajax()){
  4. returnresponse()->json(['status'=>'Ajax request']);
  5. }
  6. returnresponse()->json(['status'=>'Http request']);
  7. }
public function index()
{
    if(Request::ajax()){
        return response()->json(['status'=>'Ajax request']);
    }
    return response()->json(['status'=>'Http request']);
}

To determine if the request is over HTTPS :

  1. if(Request::secure())
  2. {
  3. //
  4. }
if (Request::secure())
{
    //
}

The Request class provides many method to examine HTTP request for your application.

Label :

PHP

Laravel PHP Framework

How To

MVC

Web Development

Hope this code and post will helped you for implement How to check request is Ajax or not in Laravel 5?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  6  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US