Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel 7 Import Export Excel & Csv File Example Tutorial

Laravel 7 Import Export Excel & Csv File Example Tutorial

Posted on March 27, 2020December 16, 2022 By XpertPhp 1 Comment on Laravel 7 Import Export Excel & Csv File Example Tutorial

In this tutorial, we will tell you how to import export excel or CSV in the Laravel Framework (Laravel 7 Import Export Excel & CSV file example tutorial).
In this example, we can easily import-export and download the excel & CSV file from the database using the maatwebsite/excel composer package. so you can follow the below step.

Overview

Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Install Package
Step 4: Add providers and aliases
Step 5: Create Dummy Records and migrate the table
Step 6: Create Route
Step 7: Create Import Class
Step 8: Create Export Class
Step 9: Create Controller
Step 10: Create Blade View
Step 11: Run The Application

Step 1: Install Laravel

We are going to install laravel 7, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

1
composer create-project --prefer-dist laravel/laravel laravel7_import_export

Step 2: Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel7_import_export)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Step 3: Install Package

Now, we are going to install the Maatwebsite package using the below command.

1
composer require maatwebsite/excel

Step 4: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file

1
2
3
4
5
6
7
8
9
10
11
'providers' => [
  .......
  Maatwebsite\Excel\ExcelServiceProvider::class,
],  
 
'aliases' => [
  .......
  'Excel' => Maatwebsite\Excel\Facades\Excel::class,
 
],

Step 5: Create Dummy Records and migrate the table

Now we will first migrate the table. after then adding a dummy record in the ‘users’ table using the laravel tinker command.

1
php artisan migrate

1
2
3
php artisan tinker
factory(App\User::class, 100)->create();

Step 6: Create Route

Add the following route code in the “routes/web.php” file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('import-export', 'TestController@importExport');
Route::post('import', 'TestController@import');
Route::get('export', 'TestController@export');
?>

Step 7: Create Import Class

Now, we will create the import class using the below command.

1
php artisan make:import ImportUsers --model=User

ImportUsers.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
  
namespace App\Imports;
  
use App\User;
use Maatwebsite\Excel\Concerns\ToModel;
  
class ImportUsers implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new User([
            'name'     => $row[0],
            'email'    => $row[1],
        ]);
    }
}
?>
See also  Laravel 7 Where Clauses Methods Example Tutorial

Step 8: Create Export Class

Now, we will create the Export class using the below command.

1
php artisan make:export ExportUsers --model=User

ExportUsers.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
  
namespace App\Exports;
  
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
  
class ExportUsers implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return User::get();
    }
}
?>

Step 9: Create Controller

Now, We will create the DataTableController using the below command and paste below code in this controller.

1
php artisan make:controller DataTableController

DataTableController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
    
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Exports\ExportUsers;
use App\Imports\ImportUsers;
use Maatwebsite\Excel\Facades\Excel;
  
class TestController extends Controller
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function importExport()
    {
       return view('import');
    }
    
    /**
    * @return \Illuminate\Support\Collection
    */
    public function export()
    {
        return Excel::download(new ExportUsers, 'users.xlsx');
    }
    
    /**
    * @return \Illuminate\Support\Collection
    */
    public function import()
    {
        Excel::import(new ImportUsers, request()->file('file'));
            
        return back();
    }
}
?>

Step 10: Create Blade View

Finally, We will create import.blade.php file in the “resources/views/” folder directory and paste below code.

import.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 7 Import Export Excel & Csv file example tutorial - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.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://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<form action="{{ url('import') }}" method="POST" name="importform"
   enctype="multipart/form-data">
{{ csrf_token() }}
<div class="form-group">
<label for="file">File:</label>
<input id="file" type="file" name="file" class="form-control">
</div>
<div class="form-group">
<a class="btn btn-info" href="{{ url('export') }}">Export File</a>
</div>
<button class="btn btn-success">Import File</button>
</form>
</div>
</body>
</html>

Step 11: Run The Application

We can start the server and run this application using the below command.

1
php artisan serve

Now we will run our example using the below Url in the browser.

1
http://127.0.0.1:8000/import-export

Laravel Tags:Import CSV File in Laravel, laravel 7, Laravel 7 Eloquent, Laravel 7 Model, laravel 7 routing, Read CSV File in Laravel, Read CSV File in PHP

Post navigation

Previous Post: Laravel 7 In Get Current URL With Parameters
Next Post: Laravel 7 Cron Job Scheduling Example Tutorial

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com