We can search data using the like operator in Laravel 10 very easily. But in this tutorial, we will search or filter data between two dates in Laravel 10 application. So from this laravel wherebetween tutorial, you will see how to search data between two dates in laravel 10.

Laravel provides whereBetween() helper that accepts an array and database column and using this we can easily search or filter data between two dates. We will create a simple form and I will put two input fields one is date_from and the other is date_to. When a user clicks a search button, we will fetch data from the database according to the date_from and date_to values using whereBetween.

Let's see the preview of this laravel 10 search data between two dates from the below image.


Step 1: Download Fresh Laravel 10

In the first step of getting data by date in Laravel, download a fresh Laravel application by the following command:

composer create-project laravel/laravel example-app


Step 2: Create Migration

In this step, we need to create a migration and model for generating a employees table. So run the below command to create employees table.

php artisan make:model Employee -m


Now update the migration like below:



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
     * Run the migrations.
    public function up(): void
        Schema::create('employees', function (Blueprint $table) {

     * Reverse the migrations.
    public function down(): void


And now update the model like along with protected property guard to avoid mass assignment error:



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
    use HasFactory;

    protected $guarded = [];


Step 3: Connect database

In this step, we will connect the database, and I am going to use MySQL. So connect it like the below by updating the .env file:




Now run the migration command:

php artisan migrate


Read also: Laravel 10 Algolia Full Text Search Tutorial


Step 4:  Create Route

Now in this, I will use one route to search or filter data between two dates. you can create two route for that. 



use App\Http\Controllers\EmployeeController;
use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!

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


Step 5: Create Controller

Now in this step, we have to create an EmployeeController to define this index method to laravel where between two columns.

php artisan make:controller EmployeeController


Now update the controller like:



namespace App\Http\Controllers;

use App\Models\Employee;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Builder;

class EmployeeController extends Controller
    public function index(Request $request)
        $employees = Employee::orderBy('id', 'desc')
                $request->date_from && $request->date_to,
                function (Builder $builder) use ($request) {

        return view('employee.index', compact('employees', 'request'));



Step 6: Create Views

We are almost there. Just we have to create our list file with all the employee list with the search form.

  • index.balde.php
  • app.blade.php

So create these files inside the following path and update them like below:


<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'Laravel') }}</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<body style="background: #ddd;">
    <div id="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse" aria-expanded="false">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    <a class="navbar-brand" href="{{ url('/employee') }}" style="margin-left:190px;">Laravelia</a>
                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <ul class="nav navbar-nav">
                        <ul class="nav navbar-nav navbar-right" style="margin-right:190px;">
                            <li><a href="{{ route('login') }}">Login</a></li>
                            <li><a href="{{ route('register') }}">Register</a></li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                <ul class="dropdown-menu">
                                        <a href="{{ route('account.index') }}">Account</a>
                                        <a href="{{ route('logout') }}"
                                        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                            {{ csrf_field() }}
        <div class="container">
            <div class="col-md-2"></div>
            <div class="col-md-8">
            <div class="col-md-2"></div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>


This page for showing all the employee lists.



    @if (session()->has('success'))
        <div class="alert alert-success">
            {{ session()->get('success') }}
    <div class="panel panel-default">
        <div class="panel-body">
            <form action="{{ route('employee.index') }}" method="get">
                 <div class="row">
                    <div class="col-md-5 form-group">
                        <label for="">Date From</label>
                        <input type="date" name="date_from" class="form-control" value="{{ $request->date_from }}">
                     <div class="col-md-5 form-group">
                        <label for="">Date From</label>
                        <input type="date" name="date_to" class="form-control" value="{{ $request->date_to }}">
                     <div class="col-md-2 form-group" style="margin-top:25px;">
                        <input type="submit" class="btn btn-primary" value="Search">

            <strong>Employee List</strong>
            <a href="{{ route('employee.create') }}" class="btn btn-primary btn-xs pull-right py-0">Create Employee</a>
            <table class="table table-responsive table-bordered table-stripped" style="margin-top:10px;">
                    @foreach ($employees as $employee)
                        <td>{{ $employee->id }}</td>
                        <td>{{ $employee->name }}</td>
                        <td>{{ $employee->email }}</td>
                        <td><span type="button" class="btn btn-{{ $employee->is_active ? 'success' : 'danger' }} btn-xs py-0">{{ $employee->is_active ? 'Active' : 'Inactive' }}</span></td>
                        <td style="display:inline-flex;">
                            <a href="{{ route('employee.show',$employee->id) }}" class="btn btn-primary btn-xs py-0" style="margin:2px;">Show</a>
                            <a href="{{ route('employee.edit',$employee->id) }}" class="btn btn-warning btn-xs py-0" style="margin:2px;">Edit</a>
                            <form action="{{ route('employee.destroy',$employee->id) }}" method="post">
                                <button type="submit" class="btn btn-danger btn-xs py-0" style="margin:2px;">Delete</button>
            {{ $employees->links() }}


Now all are set to go. Now run php artisan serve command to start the development server and visit the following URL to check search data between two dates in laravel.



Read also: Laravel 10 Scout Full Text Search Example



I have tried my best to show you this laravel where date between two columns. Now we know laravel 10 search data between two dates. Hope this laravel where between two columns tutorial will help you to create how to search data between two dates in laravel?.

Category : #laravel

Tags : #laravel , #laravel search