ItSolutionStuff.com

How to Check Request is Ajax or Not in Laravel?

By Hardik Savani • April 16, 2024
Laravel Ajax

Hi Folks,

Now, let's see post of laravel check ajax request. In this article, we will implement a laravel check if request is ajax. you can understand a concept of check if request is ajax laravel. you will learn laravel ajax request check.

Sometimes, we need to check request is ajax or not in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11. 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 $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:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index(Request $request)

{

if($request->ajax()){

return response()->json(['request' => 'ajax']);

}

return response()->json(['request' => 'http']);

}

}

Example 2:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index(Request $request)

{

if(Request::ajax()){

return response()->json(['request' => 'ajax']);

}

return response()->json(['request' => 'http']);

}

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 10 Ajax CRUD Tutorial Example

Read Now →

Laravel JQuery Ajax Loading Spinner Example

Read Now →

Laravel Login and Registration using Ajax Tutorial

Read Now →

Laravel Ajax PUT Request Example Tutorial

Read Now →

Laravel Country State City Dropdown using Ajax Example

Read Now →

Laravel Fetch Data using Ajax Example

Read Now →

Laravel Shopping Add to Cart with Ajax Example

Read Now →

Laravel Delete Record using Ajax Request Example

Read Now →

Laravel Category Treeview Hierarchical Structure Example

Read Now →

How to Get Current URL in Laravel?

Read Now →

Laravel Multiple Files Download with Response Example

Read Now →

How to Check Request Method is GET or POST in Laravel?

Read Now →

How to Get Last Inserted Id in Laravel?

Read Now →