Laravel Excel import with parameter

In this article, we’ll be discussing the laravel-excel package that can be found on laravel-excel.com. It’s a task that most developers face. How do you import an excel sheet or CSV file into a database? What if you have a ton of vendors and each of them provides you with a specific format? That’s where data importers shine.

The Manual Approach to Importing Excel Files

It is always an option. You know what the database structure looks like. You know exactly which columns you have inside of your tables, which data type is necessary, and as long as you had this data type format, you could import new table data easily. Thankfully, we have technology, and we can spin up crawlers in a relatively short period of time that gets us away from this repetitive process.

Using PHP to Import Excel Files into the Database

This is yet another option. Opening a file and reading items line by line is again an option, but we want something more sophisticated. Why recreate functionality when we can just get a package that provides us with it already? That’s where Laravel Excel shines. Someone has already gone through the trouble of creating all of the functionality that we could just tap into.

Setting Up Your Laravel Project

I’m sure you know how to create a Laravel project if you’re reading this, but just in case you didn’t, read up on the official documentation and run the following command if you’re using a Mac.

curl -s "https://laravel.build/your-app-name" | bash

The only other pre-requisite that we’ll have is that we’ll use Docker for everything.

Run your migrations, since this will automatically create our

php artisan migrate
2 table, which we’ll use as part of our example.

php artisan migrate
Setting up Laravel Excel

Once you have Laravel running, get into your Laravel container. If you’re using Docker, it’s as simple as opening up your containers, clicking on the application name (in my case import-laravel-test.test) and clicking on the Cli button.

You can always click on the “Open in external terminal” link to open it up the native terminal app, which I still personally prefer.

Once you’re in, it’s time to run the following line of code to get the laravel-excel package installed.

composer require maatwebsite/excel

You might get the following error:

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }

If you do, open you

php artisan migrate
3 file and add the modify the
php artisan migrate
4 version to:

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},

You’ll need to run

php artisan migrate
5 after you make the modification.

That’s it. It’s installed.

Creating Your First Importer

Run the following to create your first importer:

php artisan make:import UsersImport --model=User

This command will create your

php artisan migrate
6 class in
php artisan migrate
7.

Mock Data

Before we start making modifications to this code, let’s look at our excel sheet that we’ll be importing. Place the

php artisan migrate
8 into your
php artisan migrate
9 folder.

This data was generated on https://www.mockaroo.com/. This is FAKE data.

User Model

We also need to know what are

php artisan migrate
2 table looks like that we’ll be importing this data into. Each row will be treated as an array of data. If we look at the first row as an array, we will get something like this:

$row = [
'Patrick',
'Spinnace',
'[email protected]',
'xGH4RNx',
];

Let’s look at our

php artisan migrate
2 migration and see what we get.

Our

composer require maatwebsite/excel
2 model defines the following fields as
composer require maatwebsite/excel
3.

protected $fillable = [
'name',
'email',
'password',
];

UsersImport

We can now make the modifications to the

php artisan migrate
6 in order to import this data. We haven’t defined where the CSV file will be imported from yet; we’ll do that next.

What does the following code say?

  • We’re going to be accepting a row of data.
  • Looking at our $row array, we know that
    composer require maatwebsite/excel
    5 ,
    composer require maatwebsite/excel
    6 ,
    composer require maatwebsite/excel
    7 , and
    composer require maatwebsite/excel
    8 .
  • We create a
    composer require maatwebsite/excel
    9 and pass it the
    PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
    0 ,
    PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
    1 , and
    PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
    2 fields. This inserts the data into the
    php artisan migrate
    2 table.

Great. We have the backbone. But where is this

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
4 coming from? We need to create a
PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
5 to trigger this import and a
PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
6 that will grab the CSV file.

UserController

The

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
7 doesn’t exist yet, so let’s create it.

php artisan make:controller UserController

Next, we’ll create our

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
8 function.

When the

PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
8 method gets called, the
"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
0 class calls its
PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
8 method and we pass it two arguments:

  • The Importer Class that we want to use,
  • and the location of the file that we’re using.

Our

php artisan migrate
8 is located in
"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
3 .

The

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
4 method will pass each row to our
php artisan migrate
6 class so that it can be processed. Once the entire thing is complete, we can redirect the user to a success message.

All that’s left is to create a route so that we can call our

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
6 method.

Defining the Route

Route::get('/import-users', [UserController::class, 'import']);

And that’s it. Navigate to your browser and watch the import occur.

Check Imported Data

Check Imported Data by going into your

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
7 container and executing the following commands.

php artisan migrate
0

One thing that you might have noticed is that the first row in our import is our header. We’ll need to remove it, but how? It’s pretty simple with Laravel-Excel.

Removing the first row from the import

We need to use the

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
8 concern and change our integer index value to key based values.

What this means is that

"require": {
"php": "^7.3 || ^8.0",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^3.0",
"laravel/tinker": "^2.7",
"maatwebsite/excel": "^3.1"
},
8 created the following
PHP Fatal error:  Declaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixed in /var/www/html/vendor/maatwebsite/excel/src/Cache/MemoryCache.php on line 62Symfony\Component\ErrorHandler\Error\FatalErrorDeclaration of Maatwebsite\Excel\Cache\MemoryCache::get($key, $default = null) must be compatible with Psr\SimpleCache\CacheInterface::get(string $key, mixed $default = null): mixedat vendor/maatwebsite/excel/src/Cache/MemoryCache.php:6258▕59▕     /**60▕      * {@inheritdoc}61▕      */ 62    public function get($key, $default = null)63▕     {64▕         if ($this->has($key)) {65▕             return $this->cache[$key];66▕         }
4 array:

php artisan migrate
1

If you look at your users table now, you’ll see that the first row is gone.

GitHub - dinocajic/package-laravel-excel: Shows example functionality with Laravel Excel

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and…

github.com

Dino Cajic is currently the Head of IT at LSBio (LifeSpan BioSciences, Inc.), Absolute Antibody, Kerafast, Everest BioTech, Nordic MUbio and Exalpha. He also serves as the CEO at MyAutoSystem. He has over a decade of software engineering experience. He has a B.S. in Computer Science and a minor in Biology. His background consists of creating enterprise level e-commerce applications, performing research based software development, and facilitating the spread of knowledge through writing.

You can connect with him on LinkedIn, follow him on Instagram, or subscribe to his Medium publication.

Read every story from Dino Cajic (and thousands of other writers on Medium). Your membership fee directly supports Dino Cajic and other writers you read. You’ll also get full access to every story on Medium.

How can I pass parameter in the laravel Excel?

As parameter We can pass variables to the view by using the second parameter inside the loadView() method.

How to import Excel data in laravel?

There are 9 easy steps to follow..
Install new laravel project..
Configure Database details and model..
Install maatwebsite/excel package..
Create Routes..
Create import class for import data..
Create an export class for export data..
Create controller..
Create blade / view files..

How to import CSV file in laravel?

For importing excel file i am using Laravel Excel..
Step 1 - Installation. To Install the Laravel Excel Package via composer run command below. ... .
Step 2 - Create an Import Class inside app/Imports. ... .
Step 3 - Update UsersImport Class. ... .
Step 4 - Handle Uploaded Excel/CSV File. ... .
12 Rarely Used Javascript APIs You Need..

How to import CSV file in Laravel 9?

Step 1: Install Laravel Project. ... .
Step 2: Configure Database Details. ... .
Step 3: Install maatwebsite/excel package. ... .
Step 4: Generate Fake Data and Migrate Table. ... .
Step 5: Create a Routes. ... .
Step 6: Create Import Class. ... .
Step 7: Create Export Class. ... .
Step 8: Create Controller..