Search form to date export PDF in Laravel 6

Search form to date export PDF in Laravel 6




In this example, we will use maatwebsite/excel composer package for import and export tasks. maatwebsite/excel provides an easy way to import and export using the database model. maatwebsite/excel updated to version 3 and they provide a great way to import-export data from the database, so first follow a few steps to get an example.
Step 1: Install Laravel 6.17 Project
In the first step, we will install Laravel 5.7 application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Maatwebsite Package
In this step we need to install Maatwebsite package via the Composer package manager, so one your terminal and fire bellow command:
composer require maatwebsite/excel
Now open config/app.php file and add service provider and alias.
config/app.php
'providers' => [
....
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
....
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
Then you have to also make publish configuration file by using the following command:
php artisan vendor:publish
It will create a new config file named "config/excel.php".

Step 3: Create Dummy Records
In this step, we have to require the "users" table with some dummy records, so we can simply import and export. So first you have to run default migration that provided by laravel using the following command:
php artisan migrate
After that we need to run the following command to generate dummy users:
php artisan tinker
factory(App\User::class, 20)->create();
Step 4: Add Routes
In this step, we need to create a route of import-export file. so open your "routes/web.php" file and add the following route.
routes/web.php
//View Page Route::get('ViewPages', 'ViewController@index'); Route::post('ViewPages', 'ViewController@index');
Step 7: Create Controller
In this step, now we should create a new controller as MyController in this path "app/Http/Controllers/MyController.php". this controller will manage all importExportView, export and import request and return a response, so put bellow content in the controller file:
app/Http/Controllers/ViewController
<?php namespace App\Http\Controllers; use DB; use PDF; use Illuminate\Http\Request; use Maatwebsite\Excel\Facades\Excel; class ViewController extends Controller { public function index(Request $req) { $method = $req->method(); if ($req->isMethod('post')) { $from = $req->input('from'); $to = $req->input('to'); if ($req->has('search')) { // select search $search = DB::select("SELECT * FROM users WHERE email_verified_at BETWEEN '$from' AND '$to'"); return view('import',['ViewsPage' => $search]); } elseif ($req->has('exportPDF')) { // select PDF $PDFReport = DB::select("SELECT * FROM users WHERE email_verified_at BETWEEN '$from' AND '$to'"); $pdf = PDF::loadView('PDF_report', ['PDFReport' => $PDFReport])->setPaper('a4', 'landscape'); return $pdf->download('PDF-report.pdf'); } } else { //select all $ViewsPage = DB::select('SELECT * FROM users'); return view('import',['ViewsPage' => $ViewsPage]); } } }
Step 8: Create Blade File
In Last step, let's create import.blade.php(resources/views/import.blade.php) for layout and we will write design code here and put the following code:
resources/views/import.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 6 Search Report</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <br> <form action="ViewPages" method="POST" enctype="multipart/form-data"> @csrf <div class="container"> <div class="row"> <label for="from" class="col-form-label">From</label> <div class="col-md-2"> <input type="date" class="form-control input-sm" id="from" name="from"> </div> <label for="from" class="col-form-label">To</label> <div class="col-md-2"> <input type="date" class="form-control input-sm" id="to" name="to"> </div> <div class="col-md-4"> <button type="submit" class="btn btn-primary btn-sm" name="search" >Search</button> <button type="submit" class="btn btn-secondary btn-sm" name="exportPDF">export PDF</button> <button type="submit" class="btn btn-success btn-sm" name="exportExcel">export Excel</button> </div> </div> </div> </form> <br> <table class="table table-dark"> <tr> <th>id</th> <th>name</th> <th>email</th> <th>email_verified_at</th> <th>created_at</th> <th>created_at</th> </tr> @foreach ($ViewsPage as $ViewsPages) <tr> <td>{{ $ViewsPages->id }}</td> <td>{{ $ViewsPages->name }}</td> <td>{{ $ViewsPages->email }}</td> <td>{{ $ViewsPages->email_verified_at }}</td> <td>{{ $ViewsPages->created_at }}</td> <td>{{ $ViewsPages->created_at }}</td> </tr> @endforeach </table> </div> </body> </html>

resources/views/PDF_report.blade.php
<!DOCTYPE html> <html> <head> <title>Laravel 6 Search Report</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> </head> <body> <table class="table table-dark"> <tr> <th>id</th> <th>name</th> <th>email</th> <th>email_verified_at</th> <th>created_at</th> <th>created_at</th> </tr> @foreach ($PDFReport as $PDFReports) <tr> <td>{{ $PDFReports->id }}</td> <td>{{ $PDFReports->name }}</td> <td>{{ $PDFReports->email }}</td> <td>{{ $PDFReports->email_verified_at }}</td> <td>{{ $PDFReports->created_at }}</td> <td>{{ $PDFReports->created_at }}</td> </tr> @endforeach </table> </body> </html>
Now you can check on your laravel 6.16 application.
I hope it can help you...
php artisan serve
http://127.0.0.1:8000/ViewPages
Reactions

Post a Comment

1 Comments

CAN FEEDBACK

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

close