Laravel 6 - Search Data Between Two Dates (Dynamic Date Filter)
In Laravel 6, you can filter data dynamically between two dates using Eloquent ORM or Query Builder. Below is a step-by-step guide to implement it.
1. Setup Database & Model
Ensure your database is properly set up in .env
file:
If you don’t have a table, create a migration for a sample orders
table:
Modify database/migrations/xxxx_xx_xx_create_orders_table.php
:
Run migration:
Create an Eloquent model for Order
:
Modify app/Order.php
:
2. Create Controller for Date Filtering
Run the command to create a controller:
Modify app/Http/Controllers/OrderController.php
:
3. Create Blade View for Date Search Form
Create resources/views/orders/index.blade.php
:
4. Define Route
Modify routes/web.php
:
5. Run the Application
Start the Laravel development server:
Visit:
-
Select From Date and To Date, then click Search.
-
It will display the filtered orders based on the selected date range.
6. Alternative: Using Query Builder
If you prefer Query Builder instead of Eloquent:
Modify OrderController.php
:
Conclusion
✅ Dynamic date search using whereBetween()
.
✅ Eloquent ORM or Query Builder for flexibility.
✅ Simple HTML form for user-friendly filtering.