Cara menggunakan maatwebsite/excel laravel 9

Laravel Excel is designed at being a Laravel-flavoured PhpSpreadsheet. It is a manageable and elegant wrapper around PhpSpreadsheet to simplify exports and imports. PhpSpreadsheet is a php based library that enables you to read and write different spreadsheet file formats, like Excel and LibreOffice Calc. Laravel Excel has the following features:

  • Easily export collections to Excel.
  • Export queries with automatic chunking for better performance.
  • Queue exports for better performance.
  • Easily export Blade views to Excel.
  • Easily import to collections.
  • Read the Excel file in chunks.
  • Handle the import inserts in batches.

If you want to create easy import and export, excel file functionality, this laravel maatwebsite/excel tutorial is best for you.

At the end of this tutorial, you will be able to download or import excel & CSV files directly from the database in laravel application.

Requirements

  • PHP: 
    laravel new laravel-excel
    9
  • Laravel: 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    0
  • PhpSpreadsheet: 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    1
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    2 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    3 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    4 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    5 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    6 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    7 enabled
  • PHP extension 
    DB_CONNECTION=mysql 
    DB_HOST=127.0.0.1 
    DB_PORT=3306 
    DB_DATABASE=
    DB_USERNAME=
    DB_PASSWORD=
    8 enabled

Step 1: Install Laravel Project

First, open Terminal and run the following command to create a fresh laravel project:

composer create-project --prefer-dist laravel/laravel laravel-excel

or, if you have installed the Laravel Installer as a global composer dependency:

laravel new laravel-excel

Step 2: Configure Database Details

After, Installation Go to the project root directory, open .env file, and set database detail as follow:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=

Read Also: Laravel 9 CRUD Example Tutorial for Beginners

Step 3: Install maatwebsite/excel package

You can install Laravel Excel via composer. You've to run this command for the installation.

composer require maatwebsite/excel

If composer require fails on Laravel 9 because of the 

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
9 dependency, you will have to specify the 
composer require maatwebsite/excel
0 version as 
composer require maatwebsite/excel
1 in your composer.json to satisfy the PhpSpreadsheet dependency. You can install both at the same time as:

composer require psr/simple-cache:^2.0 maatwebsite/excel

Register Plugin’s Service in Providers & Aliases

You can have the following code placed inside the config/app.php file.

'providers' => [
  Maatwebsite\Excel\ExcelServiceProvider::class,
 ],  

'aliases' => [ 
  'Excel' => Maatwebsite\Excel\Facades\Excel::class,
], 

Execute the vendor, publish the command, and publish the config.

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config

This will create a new config file named 

composer require maatwebsite/excel
2.

Step 4: Generate Fake Data and Migrate Table

In the First step, We migrate the user table. After migration run successfully We moved to the second step.

php artisan migrate

In the Second Step, We generate the fake record. Here We use tinker to generate the fake records. You can use a different method as of your requirement.

php artisan tinker

After Opening the tinker, you need to run this command to generate the fake records in our database.

User::factory()->count(100)->create();

Step 5: Create a Routes

In this step, We will add a route to handle requests for import and export files.

laravel new laravel-excel
0

Step 6: Create Import Class

Maatwebsite provides a way to build an import class and we have to use it in the controller. So it would be a great way to create a new Import class. So you have to run the following command and change the following code on that file:

laravel new laravel-excel
1

composer require maatwebsite/excel
3

laravel new laravel-excel
2

Here you can see map CSV or excel column value to our Eloquent Model. You need to format that CSV or excel column as you map in your import class.

Read Also: How to Install MongoDB on Ubuntu 20.04

Step 7: Create Export Class

Maatwebsite provides a way to build an export class and we have to use it in the controller. So it would be a great way to create a new export class. So you have to run the following command and change the following code on that file:

laravel new laravel-excel
3

composer require maatwebsite/excel
4

laravel new laravel-excel
4

Step 8: Create Controller

Next, We have to create a controller to display a form to upload CSV or excel file records. Let's Create a controller named

composer require maatwebsite/excel
5 using the command given below:

laravel new laravel-excel
5

Once the above command execute, it will create a controller file UserController.php in the app/Http/Controllers directory. Open the UserController.php file and put this code into that file.

laravel new laravel-excel
6

Step 9: Create Blade / View Files

We have reached the last step. In general, here we need to formulate the view for handling importing and exporting through the frontend. Create a resources/views/importFile.blade.php file to set up the view. Place the following code inside the blade view file:

laravel new laravel-excel
7

Run Laravel Application

Lastly, we have to run the Laravel application, for this, we have to go to the command prompt, and write the following command:

laravel new laravel-excel
8

After executing this command, Open http://localhost:8000/file-import in your browser.

Thank you for reading this blog.

Read Also: How To Install Vue 3 in Laravel 9 From Scratch

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.