Search form to date export PDF in Laravel 6

Search form to date export PDF in Laravel 6


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:

composer require barryvdh/laravel-dompdf

Add the service provider and alias in config/app.php (for Laravel 6):

'providers' => [ Barryvdh\DomPDF\ServiceProvider::class, ], 'aliases' => [ 'PDF' => Barryvdh\DomPDF\Facade::class, ],

Publish the package configuration (optional):

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

2. Create Database Table and Model

Ensure your database credentials are set in .env:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database DB_USERNAME=root DB_PASSWORD=

Create Migration for Orders Table

php artisan make:migration create_orders_table

Modify database/migrations/xxxx_xx_xx_create_orders_table.php:

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateOrdersTable extends Migration { public function up() { Schema::create('orders', function (Blueprint $table) { $table->id(); $table->string('customer_name'); $table->decimal('amount', 10, 2); $table->date('order_date'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('orders'); } }

Run migration:

php artisan migrate

Create an Order Model

php artisan make:model Order

Modify app/Order.php:

namespace App; use Illuminate\Database\Eloquent\Model; class Order extends Model { protected $fillable = ['customer_name', 'amount', 'order_date']; }

3. Create Controller

php artisan make:controller OrderController

Modify app/Http/Controllers/OrderController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Order; use Barryvdh\DomPDF\Facade as PDF; class OrderController extends Controller { public function index(Request $request) { $query = Order::query(); // If 'from' and 'to' dates are provided, apply the filter if ($request->has('from') && $request->has('to')) { $from = $request->input('from'); $to = $request->input('to'); $query->whereBetween('order_date', [$from, $to]); } $orders = $query->get(); return view('orders.index', compact('orders')); } public function exportPDF(Request $request) { $query = Order::query(); if ($request->has('from') && $request->has('to')) { $query->whereBetween('order_date', [$request->input('from'), $request->input('to')]); } $orders = $query->get(); $data = [ 'title' => 'Order Report', 'date' => date('m/d/Y'), 'orders' => $orders ]; $pdf = PDF::loadView('orders.pdf', $data); return $pdf->download('orders_report.pdf'); } }

4. Create Blade View for Search Form

Create resources/views/orders/index.blade.php:

<!DOCTYPE html> <html> <head> <title>Search Orders and Export PDF</title> </head> <body> <h2>Search Orders</h2> <form method="GET" action="{{ url('/orders') }}"> <label>From Date:</label> <input type="date" name="from" required> <label>To Date:</label> <input type="date" name="to" required> <button type="submit">Search</button> </form> @if(isset($orders)) <h3>Order Results</h3> <table border="1"> <tr> <th>ID</th> <th>Customer Name</th> <th>Amount</th> <th>Order Date</th> </tr> @foreach ($orders as $order) <tr> <td>{{ $order->id }}</td> <td>{{ $order->customer_name }}</td> <td>${{ $order->amount }}</td> <td>{{ $order->order_date }}</td> </tr> @endforeach </table> <form method="GET" action="{{ url('/orders/export-pdf') }}"> <input type="hidden" name="from" value="{{ request('from') }}"> <input type="hidden" name="to" value="{{ request('to') }}"> <button type="submit">Export to PDF</button> </form> @endif </body> </html>

5. Create PDF Blade View

Create resources/views/orders/pdf.blade.php:

<!DOCTYPE html> <html> <head> <title>Order Report</title> <style> body { font-family: Arial, sans-serif; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid black; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h2>{{ $title }}</h2> <p>Date: {{ $date }}</p> <table> <tr> <th>ID</th> <th>Customer Name</th> <th>Amount</th> <th>Order Date</th> </tr> @foreach($orders as $order) <tr> <td>{{ $order->id }}</td> <td>{{ $order->customer_name }}</td> <td>${{ $order->amount }}</td> <td>{{ $order->order_date }}</td> </tr> @endforeach </table> </body> </html>

6. Define Routes

Modify routes/web.php:

use App\Http\Controllers\OrderController; Route::get('/orders', [OrderController::class, 'index']); Route::get('/orders/export-pdf', [OrderController::class, 'exportPDF']);

7. Run the Application

Start Laravel development server:

php artisan serve

Test the Application

  1. Visit:

    http://127.0.0.1:8000/orders
  2. Select From Date and To Date, then click Search.

  3. the results appear, click "Export to PDF" to download the report.

8. Additional Features

  • Show PDF in Browser Instead of Downloading

    return $pdf->stream('orders_report.pdf');
  • Customize Paper Size & Orientation

    $pdf = PDF::loadView('orders.pdf', $data)->setPaper('a4', 'landscape');

Conclusion

Dynamic date search using whereBetween().
Dompdf integration for PDF export.
User-friendly UI with simple form input.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

1 Comments

CAN FEEDBACK
  1. Rowena
    Rowena
    not complete PDF print not working
close