How To Get Data Between Two Dates In Laravel

In this quick example i will show you how to find data between two dates in laravel. So you will learn from scratch that how to fetch data date wise from database in laravel. So if you don't know how to get data between two dates in Laravel then this example tutorial is for you.

In this example i will also discuss about how to get data between two dates in Laravel. So after finishing this project you will also learn where between laravel query. We will find or fetch data between two dates using whereBetween.

I will use whereBetween laravel in our query and fetch data from database. In this tutorial have a look on below image of preview of this tutorial.

data-between-tow-date-laravel

 

Step 1: Download Laravel

To build fetch data between two dates in Laravel download a fresh laravel project by the following command.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2 : Create Route

Now we need routes to see our input page where i will give two date input. So create this below route and create some dummy data of users.

routes/web.php

Route::get('/', 'TestController@index');
Route::get('/report', 'TestController@daily_report')->name('report');

 

Step 3: Add Controller 

Ok, now we have to add new controller method "index()"  and daily_report() in your TestController, so if you haven't created TestController then you can create it and paste this following code.

app/Http/Controllers/TestController.php

namespace App\Http\Controllers;

use App\Mail\CheckUser;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use PDF;

class TestController extends Controller
{
    public function index()
    {   
       return view('welcome');
    }

    public function daily_report(Request $request)
    {
       $start_date = Carbon::parse($request->start_date)
                             ->toDateTimeString();

       $end_date = Carbon::parse($request->end_date)
                             ->toDateTimeString();

       return User::whereBetween('created_at',[$start_date,$end_date])->get();

    }
}

 

Step 4: Create View

In this step we need to create to blade file. So modify welcome file as like below.

resources/views/welcome.blade.php

 

Read also : Laravel Generate PDF and Send Email Example

 

In this example I will also showed how to fetch database data between two dates in laravel. Hope it can help you.

 

#laravel #laravel-7 #laravel-eloquent #laravel-carbon