Laravel 6 - Search Data by Date Range and Export to PDF Using DomPDF
In this tutorial, you’ll learn how to filter records based on a date range and export the results as a PDF using the Dompdf package in Laravel 6.
1. Install DomPDF Package
Run the command to install barryvdh/laravel-dompdf:
Add the service provider and alias in config/app.php
(for Laravel 6):
Publish the package configuration (optional):
2. Create Database Table and Model
Ensure your database credentials are set in .env
:
Create Migration for Orders Table
Modify database/migrations/xxxx_xx_xx_create_orders_table.php
:
Run migration:
Create an Order Model
Modify app/Order.php
:
3. Create Controller
Modify app/Http/Controllers/OrderController.php
:
4. Create Blade View for Search Form
Create resources/views/orders/index.blade.php
:
5. Create PDF Blade View
Create resources/views/orders/pdf.blade.php
:
6. Define Routes
Modify routes/web.php
:
7. Run the Application
Start Laravel development server:
Test the Application
-
Visit:
-
Select From Date and To Date, then click Search.
-
the results appear, click "Export to PDF" to download the report.
8. Additional Features
-
Show PDF in Browser Instead of Downloading
-
Customize Paper Size & Orientation
Conclusion
✅ Dynamic date search using whereBetween()
.
✅ Dompdf integration for PDF export.
✅ User-friendly UI with simple form input.