Laravel Datatables Date Range Filter Example

By Hardik Savani April 16, 2024 Category : Laravel

Hey Developer,

If you need to see an example of laravel datatable date range filter. In this article, we will implement a yajra datatables date range filter. This post will give you a simple example of laravel datatables daterangepicker example. you can understand a concept of how to add filter date range in laravel datatables. Alright, let’s dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Yajra Datatables provides us with a quick search, pagination, ordering, sorting and etc. Datatables are basically jQuery plugins that allow you to add advanced interaction controls to your HTML tables data. Datatables also provide ajax for data searching and getting. you can give a very quick layout for searching and sorting using Datatables. You can also implement Datatables in your laravel application.

In this example, we will use the default "users" table and add some dummy users to it using tinker, then we simply list all users using yajra data tables. then we will use bootstrap daterangepicker to filter with created_at date column. so let's follow the below step and make it done.

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install Yajra Datatable

In this step we need to install yajra datatable via the Composer package manager, so one your terminal and fire bellow command:

composer require yajra/laravel-datatables-oracle

Step 3: Add Dummy Users

In this step, we will create some dummy users using tinker factory. so let's create dummy records using bellow command:

php artisan tinker


Step 4: Create Controller

In this point, now we should create new controller as UserController. this controller will manage layout and getting data request and return response, so put bellow content in controller file:



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use DataTables;

class UserController extends Controller



* Display a listing of the resource.


* @return \Illuminate\Http\Response


public function index(Request $request)


if ($request->ajax()) {

$data = User::select('*');

if ($request->filled('from_date') && $request->filled('to_date')) {

$data = $data->whereBetween('created_at', [$request->from_date, $request->to_date]);


return Datatables::of($data)


->addColumn('action', function($row){

$btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View</a>';

return $btn;





return view('users');



Step 5: Add Route

In this is step we need to create route for datatables layout file and another one for getting data. so open your "routes/web.php" file and add following route.



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('users', [UserController::class, 'index'])->name('users.index');

Step 6: Create Blade File

In Last step, let's create users.blade.php(resources/views/users.blade.php) for layout and we will write design code here and put following code:


<!DOCTYPE html>



<title>Laravel Yajra Datatables Tutorial -</title>

<meta name="csrf-token" content="{{ csrf_token() }}">

<link href="" rel="stylesheet">

<link href="" rel="stylesheet">

<script src=""></script>

<script src=""></script>

<script src=""></script>

<script src="" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<script src=""></script>

<script type="text/javascript" src=""></script>

<script type="text/javascript" src=""></script>

<link rel="stylesheet" type="text/css" href="" />



<div class="container">

<h1>Laravel Datatables Date Range Filter Example -</h1>

<div style="margin: 20px 0px;">

<strong>Date Filter:</strong>

<input type="text" name="daterange" value="" />

<button class="btn btn-success filter">Filter</button>


<table class="table table-bordered data-table" >






<th width="100px">Action</th>








<script type="text/javascript">

$(function () {


startDate: moment().subtract(1, 'M'),

endDate: moment()


var table = $('.data-table').DataTable({

processing: true,

serverSide: true,

ajax: {

url: "{{ route('users.index') }}",

data:function (d) {

d.from_date = $('input[name="daterange"]').data('daterangepicker').startDate.format('YYYY-MM-DD');

d.to_date = $('input[name="daterange"]').data('daterangepicker').endDate.format('YYYY-MM-DD');



columns: [

{data: 'id', name: 'id'},

{data: 'name', name: 'name'},

{data: 'email', name: 'email'},

{data: 'action', name: 'action', orderable: false, searchable: false},









Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:



I hope it can help you...

Tags :