In this tutorial, I would like to share with you how to export and import Excel spreadsheet or CSV file to the database in PHP laravel 6.16 framework. I will show you step by step examples of import CSV or excel file and export CSV or excel file using maatwebsite/excel version 3 composer package.
We almost require to implement excel or CSV file upload for data into users' tables, products table etc. because it helps to add multiple records at the same time. So if you are working on a big project then you almost require to upload data using CSV file. You also need to give features to export download CSV files. If you have the same requirement then follow this article and make it for you.
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.7 Project
In 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 tinkerfactory(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]); } } 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
Read Also: Laravel 5.7 Guzzle HTTP client POST request example
<!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> </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>
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
0 Comments
CAN FEEDBACK
Emoji