Installation Laravel Excel in laravel 10.

Updated: Jan 26, 2025

Installation Laravel Excel in laravel 10.

To install Laravel Excel in Laravel 10, you'll need to follow these steps:

  1. Install Laravel Excel Package:

Laravel Excel is a package developed by Maatwebsite, which provides an easy way to import and export Excel files in Laravel. To install this package, you can use Composer, the dependency manager for Laravel.

First, make sure you have Composer installed on your system. If not, download and install it from the official website: https://getcomposer.org/

Next, open your terminal or command prompt and navigate to your Laravel project directory. Run the following command to install the Laravel Excel package:

composer require maatwebsite/excel

This command will download and install the Laravel Excel package and its dependencies.

  1. Import the Service Provider:

After installing the package, you need to register the service provider in your Laravel application. Open the config/app.php file and add the following line to the providers array:

Maatwebsite\Excel\ExcelServiceProvider::class,

Next, open the app/Providers/AppServiceProvider.php file and add the following lines to the boot() method:

use Maatwebsite\Excel\Facades\Excel;

Excel::macro('import', function ($class) {
    return app($class);
});

This code imports the Excel facade and defines a macro called import that will be used to simplify the import process.

  1. Create a New Excel Import/Export Controller:

Now, you can create a new controller to handle your Excel imports and exports. Run the following command to create a new controller:

php artisan make:controller ExcelController --resource

Next, open the newly created app/Http/Controllers/ExcelController.php file and add the following import statement at the top:

use Maatwebsite\Excel\Facades\Excel;

Now, you can define methods to handle your imports and exports. For example, to import data from an Excel file, you can define a method like this:

public function import(Request $request)
{
    $file = $request->file('file');
    $data = Excel::import(new YourImportClass, $file);

    // Process the imported data here

    return back();
}

Replace YourImportClass with the name of the import class you'll create later.

  1. Create a New Excel Import Class:

To define the logic for importing data from an Excel file, you need to create a new import class. Run the following command to create a new file:

php artisan make:import YourImportClass --model=YourModel

Replace YourImportClass with the name of the import class and YourModel with the name of the model that represents the data you want to import.

Next, open the newly created app/Imports/YourImportClass.php file and define the mapping property to map the columns in the Excel file to the properties of your model:

protected $map = [
    'column1' => 'property1',
    'column2' => 'property2',
    // ...
];

Replace column1 and column2 with the names of the columns in the Excel file and property1 and property2 with the names of the corresponding properties in your model.

  1. Test Your Import:

Finally, you can test your import by visiting the route that handles the import method in your controller. For example, if your import method is located at /import, you can test it by visiting http://localhost/import in your web browser and selecting an Excel file to upload.

That's it! You've successfully installed and configured Laravel Excel in Laravel 10. You can now use this package to import and export Excel files in your Laravel application.