Laravel 5.5 import export to excel and CSV using maatwebsite example.

Laravel 5.5 import export to excel and CSV using maatwebsite example.

Hello, Techies In this Quick Tutorial, I am going to tell you how you can import excel and CSV file in Laravel 5.5 framework. Generally, if we talk about Import, export download excel files so there is no need to write much code to handle that. Laravel provides a great rich set of the package that is maatwebsite package where you can easily get data and perform operations like import, export and also it can handle more than one sheet.

So in this quick example, we will discuss here import data for xls, xlsx, and CSV format files. Here we will discuss it in few steps.

Installation for the maatwebsite

Open your composer.json file and add the following.


"maatwebsite/excel": "~2.1.0"

After that, you need to run the composer update to add this package this to the application.

$ Composer update

#2:- Now, the next step is to add providers and alised into config file.. you can locate it into app->config.php file


'providers' => [
	....

	'Maatwebsite\Excel\ExcelServiceProvider',

],

'aliases' => [

	....

	'Excel' => 'Maatwebsite\Excel\Facades\Excel',

],

Run the vendor publish command for config.



php artisan vendor:publish

#1:- View file=> Create your view file inside Resources->view folder importExport.blade.php


	<div class="container">		
		<div class="panel panel-primary">
		  <div class="panel-heading">
		    <h3 class="panel-title" style="padding:12px 0px;font-size:25px;"><strong>Import</h3>
		  </div>
		  <div class="panel-body">

				<h3>Import File Form:</h3>
				<form action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" enctype="multipart/form-data">

					<input type="file" name="sample_file" />
					{{ csrf_field() }}
					<br/>

					<button class="btn btn-primary">Import CSV or Excel File</button>
					<a class="btn btn-success" href="public/uploads/pullist.xlsx">Download Sample Excel</a>
				</form>
				

		  </div>
		</div>
	
	</div>

#2:- Controller file MaatwebsiteDemoController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Excel;
use Validator;
use Session;
use App\Http\Requests;
use DB;

class MaatwebsiteDemoController extends Controller
{

    /**
     * Return View file
     *
     * @var array
     */
    public function importExport()
    {
        //return view('importExport');
    }


    /**
     * Import file into database Code
     *
     * @var array
     */
    public function importExcel(Request $request)
    {

        if($request->hasFile('sample_file')){
            $path = $request->file('sample_file')->getRealPath();
            $data = Excel::load($path)->get();
            if($data->count()){
                foreach ($data as $key => $value) {
                    $arr[] = ['comic_title' => $value->comic_title,
                              'cover_artist' => $value->cover_artist,
                              'comic_writer' => $value->comic_writer, 
                              'comic_publisher' => $value->comic_publisher,
                              'comic_description' => $value->comic_description,
                              'comic_issue_no' => $value->comic_issue_no,
                              'comic_release_date' => $value->comic_release_date,
                              'tags' => $value->tags,
                              'created_by' =>Auth::guard('inventoryadmin')->user()->id
                              ];
                }
                if(!empty($arr))
                {
                    \DB::table('comic_info')->insert($arr);
                    return redirect('inventoryadmin/dashboard')->with('message','Comic Records Successfully Imported');
                }
            }
        }
        dd('Request data does not have any files to import.');  
    }

}

#3:- Create your routes inside route->web.php file.

Route::post('importExcel', 'MaatwebsiteDemoController@importExcel');

 Route::get('/excel', function () {
    return view('importExport');
});

Now just run your laravel server php artisan serve and hit the url localhost:8000/excel

Krishna Gupta

Krishna Ballabh Gupta is a Gold Medalist Full Stack Developer & Algorithm Designer and Founder of TechnologyShouters. He generally Blogs about Python, JavaScript, jQuery, AngularJS, Database, PHP, AJAX and something that interesting.

Related Posts

Create Account



Log In Your Account



1Subscribe to Our Live Updates.