onlinecode

Laravel 6 Import Export Excel & Csv file example tutorial – onlinecode

Laravel 6 Import Export Excel & Csv file example tutorial – onlinecode

In this post we will give you information about Laravel 6 Import Export Excel & Csv file example tutorial – onlinecode. Hear we will give you detail about Laravel 6 Import Export Excel & Csv file example tutorial – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this tutorial, we will tell you how to import export excel or CSV in the Laravel Framework (Laravel 6 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 LaravelStep 2: Setting Database ConfigurationStep 3: Install PackageStep 4: Add providers and aliasesStep 5: Create Dummy Records and migrate the tableStep 6: Create RouteStep 7: Create Import ClassStep 8: Create Export ClassStep 9: Create ControllerStep 10: Create Blade ViewStep 11: Run The Application

Step 1: Install Laravel

We are going to install laravel 6, 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.

composer create-project --prefer-dist laravel/laravel larave6_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.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(larave6_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.

composer require maatwebsite/excel

Step 4: Add providers and aliases

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

'providers' => [
  .......
  MaatwebsiteExcelExcelServiceProvider::class,
 
 ],  

'aliases' => [ 
  .......
  'Excel' => MaatwebsiteExcelFacadesExcel::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.

php artisan migrate

php artisan tinker
 
factory(AppUser::class, 100)->create();

Step 6: Create Route

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

<?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.

php artisan make:import ImportUsers --model=User

ImportUsers.php

<?php
   
namespace AppImports;
   
use AppUser;
use MaatwebsiteExcelConcernsToModel;
   
class ImportUsers implements ToModel
{
    /**
    * @param array $row
    *
    * @return IlluminateDatabaseEloquentModel|null
    */
    public function model(array $row)
    {
        return new User([
            'name'     => $row[0],
            'email'    => $row[1], 
        ]);
    }
}
?>

Step 8: Create Export Class

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

php artisan make:export ExportUsers --model=User

ExportUsers.php

<?php
   
namespace AppExports;
   
use AppUser;
use MaatwebsiteExcelConcernsFromCollection;
   
class ExportUsers implements FromCollection
{
    /**
    * @return IlluminateSupportCollection
    */
    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.

php artisan make:controller DataTableController

DataTableController.php

<?php
    
namespace AppHttpControllers;
   
use IlluminateHttpRequest;
use AppExportsExportUsers;
use AppImportsImportUsers;
use MaatwebsiteExcelFacadesExcel;
   
class TestController extends Controller
{
    /**
    * @return IlluminateSupportCollection
    */
    public function importExport()
    {
       return view('import');
    }
    
    /**
    * @return IlluminateSupportCollection
    */
    public function export() 
    {
        return Excel::download(new ExportUsers, 'users.xlsx');
    }
    
    /**
    * @return IlluminateSupportCollection
    */
    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

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 6 Import Export Excel & Csv file example tutorial - onlinecode</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 >
	<form action="{{ url('import') }}" method="POST" name="importform"
	   enctype="multipart/form-data">
		{{ csrf_token() }}
		<div >
			<label for="file">File:</label>
			<input id="file" type="file" name="file" >
		</div>
		<div >
			<a  href="{{ url('export') }}">Export File</a>
		</div> 
		<button >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.

php artisan serve

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


Please follow and like us:

Hope this code and post will helped you for implement Laravel 6 Import Export Excel & Csv file example tutorial – onlinecode. 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