Import and Export CSV file in Laravel 5.8

Import and Export CSV file in Laravel 5.8

In this post we will give you information about Import and Export CSV file in Laravel 5.8. Hear we will give you detail about Import and Export CSV file in Laravel 5.8And how to use it also give you demo for it if it is necessary.

import export excel or csv from database is a primary requirement for admin project. Here, i will give you very simple step by step tutorial of import csv or excel file and export csv or excel file using maatwebsite/excel version 3 in laravel 5.8 application.

We will simple create import data to csv, xls file and also we can import data to database using csv file in laravel 5.8 application.

In this example we will use maatwebsite/excel composer package for import and export task. maatwebsite/excel provide easy way to import and export using database model. maatwebsite/excel updated version 3 and they provide great way to import export data from database, so first follow few step to get example.

Step 1 : Install Laravel 5.8 Project

Here, we need install Laravel 5.8 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 aliase.

config/app.php

'providers' => [

....

MaatwebsiteExcelExcelServiceProvider::class,

],

'aliases' => [

....

'Excel' => MaatwebsiteExcelFacadesExcel::class,

],

Also see:Laravel 5.8 CRUD (Create Read Update Delete) Tutorial For Beginners

Step 3: Create Dummy Records

In this step, we have to require “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 following command:

php artisan migrate

After that we need to run following command to generate dummy users:

php artisan tinker

factory(AppUser::class, 20)->create();

Step 4: Add Routes

In this step, we need to create route of import export file. so open your “routes/web.php” file and add following route.

routes/web.php

Route::get('export', 'MyController@export')->name('export');

Route::get('importExportView', 'MyController@importExportView');

Route::post('import', 'MyController@import')->name('import');


Step 5: Create Import Class

In maatwebsite 3 version provide way to built import class and we have to use in controller. So it would be great way to create new Import class. So you have to run following command and change following code on that file:

php artisan make:import UsersImport --model=User

app/Imports/UsersImport.php

<?php

namespace AppImports;

use AppUser;

use MaatwebsiteExcelConcernsToModel;

use MaatwebsiteExcelConcernsWithHeadingRow;

class UsersImport implements ToModel, WithHeadingRow

{

/**

* @param array $row

*

* @return IlluminateDatabaseEloquentModel|null

*/

public function model(array $row)

{

return new User([

'name' => $row['name'],

'email' => $row['email'],

'password' => Hash::make($row['password']),

]);

}

}

You can download demo csv file from here: Demo CSV File.

Step 6: Create Export Class

maatwebsite 3 version provide way to built export class and we have to use in controller. So it would be great way to create new Export class. So you have to run following command and change following code on that file:

php artisan make:export UsersExport --model=User

app/Exports/UsersExport.php

<?php

namespace AppExports;

use AppUser;

use MaatwebsiteExcelConcernsFromCollection;

class UsersExport implements FromCollection

{

/**

* @return IlluminateSupportCollection

*/

public function collection()

{

return User::all();

}

}

Step 7: Create Controller

In this step, now we should create new controller as MyController in this path “app/Http/Controllers/MyController.php”. this controller will manage all importExportView, export and import request and return response, so put bellow content in controller file:

app/Http/Controllers/MyController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppExportsUsersExport;

use AppImportsUsersImport;

use MaatwebsiteExcelFacadesExcel;

class MyController extends Controller

{

/**

* @return IlluminateSupportCollection

*/

public function importExportView()

{

return view('import');

}

/**

* @return IlluminateSupportCollection

*/

public function export()

{

return Excel::download(new UsersExport, 'users.xlsx');

}

/**

* @return IlluminateSupportCollection

*/

public function import()

{

Excel::import(new UsersImport,request()->file('file'));

return back();

}

}

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 following code:

resources/views/import.blade.php

Also see:Task Scheduling with Cron Job in Laravel 5.8

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.8 Import Export Excel to database Example - ItSolutionStuff.com</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />

</head>

<body>

<div >

<div >

<div >

Laravel 5.8 Import Export Excel to database Example - ItSolutionStuff.com

</div>

<div >

<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">

@csrf

<input type="file" name="file" >

<br>

<button >Import User Data</button>

<a href="{{ route('export') }}">Export User Data</a>

</form>

</div>

</div>

</div>

</body>

</html>

Now you can check on your laravel 5.8 application.

I hope it can help you…

Hope this code and post will helped you for implement Import and Export CSV file in Laravel 5.8. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

49  +    =  59

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US