Today, We want to share with you Laravel Excel and csv Import Export Example.In this post we will show you Import & Export Data in Laravel Excel, hear for Laravel 5 import export to excel and csv using maatwebsite example.
we will give you demo and example for implement.In this post, we will learn about Import & Export Data in CSV in Laravel 5 with an example.
Laravel Excel and csv import export using maatwebsite
There are the Following The simple About Laravel Excel and csv Import Export Full Information With Example and source code.
Step 1: Laravel Installation for laravel excel import export
Laravel 5
"maatwebsite/excel": "~2.1.0"
Laravel 4
"maatwebsite/excel": "~1.3"
Laravel-Excel
composer update
and then open this file config/app.php and put this service provider and some aliase.
'providers' => [ .... ..... ...... 'Maatwebsite\Excel\ExcelServiceProvider', ], 'aliases' => [ .... ..... ...... 'Excel' => 'Maatwebsite\Excel\Facades\Excel', ],
Config for Laravel 5 excel then fire following command
php artisan vendor:publish
E-junkie: Sell digital downloads online
E-junkie Provides a Copy-paste buy-now, and cart buttons for selling downloads, codes and tangible products on any website, blog, social media, email and messenger!
Also see:
Config for Laravel 4 excel then fire following command
php artisan config:publish maatwebsite/excel
Step 2: Make Table and Model
make a migration for products table
php artisan make:migration create_products_table
Path:- database/migrations
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateItemsTable extends Migration { public function up() { Schema::create('products', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('information'); $table->timestamps(); }); } public function down() { Schema::drop("products"); } }
model for Items – app/Item.php
namespace App; use Illuminate\Database\Eloquent\Model; class Item extends Model { public $fillable = ['title','information']; }
Step 3: Create Route
file Path : app/Http/routes.php
Route::get('laravelimportExport', '[email protected]'); Route::get('laravelExcelDownload/{type}', '[email protected]'); Route::post('laraveImportlExcel', '[email protected]');
Step 4: Create Controller
app/Http/Controllers/MaatwebsiteDemoController.php
use Input; use App\Item; use DB; use Excel; class MaatwebsiteDemoController extends Controller { public function laravelimportExport() { return view('laravelimportExport'); } public function laravelExcelDownload($type) { $data = Item::get()->toArray(); return Excel::create('infinityknow_example', function($excel) use ($data) { $excel->sheet('mySheet', function($sheet) use ($data) { $sheet->fromArray($data); }); })->download($type); } public function laraveImportlExcel() { if(Input::hasFile('import_file')){ $path = Input::file('import_file')->getRealPath(); $data = Excel::load($path, function($reader) { })->get(); if(!empty($data) && $data->count()){ foreach ($data as $key => $value) { $insert[] = ['title' => $value->title, 'information' => $value->information]; } if(!empty($insert)){ DB::table('products')->insert($insert); dd('Insert Record successfully.'); } } } return back(); } }
Step 5: Create View
laravelimportExport.blade.php
<title>Excel and csv import export using maatwebsite in laravel example</title> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Import - Export in Excel and CSV Laravel 5</a> </div> </div> </nav> <div class="container laravel excel"> <a href="to('laravelExcelDownload/xls') }}"><button class="btn btn-success">Download Excel xls</button></a> <a href="to('laravelExcelDownload/xlsx') }}"><button class="btn btn-success">Download Excel xlsx</button></a> <a href="to('laravelExcelDownload/csv') }}"><button class="btn btn-success">Download CSV</button></a> <form style="border: 4px solid #a1a1a1;margin-top: 20px;padding: 15px" action="to('laraveImportlExcel') }}" class="infinity form-horizontal" method="post" enctype="multipart/form-data"> <button class="btn btn-success">laravel excel Import File</button> </form> </div>
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about Laravel 5 import export to excel and csv.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.