Hello Artisan,
In this tutorial, I will show you how to import and export csv and excel file data in Laravel 9 applications using maatwebsite/excel
package. You know that it is a common requirement in our web applications that export and import features.
So I am here like before to show you an example of laravel 9 import export excel file example. This tutorial will give you a complete step by step beginner example of laravel 9 import export csv file. We will see from scratch an example of import export csv file in laravel 9. This laravel csv and excel article will give you a simple example of laravel 9 import file excel.
I will use maatwebsite/excel
composer package for import and export data in Laravel. In this example, I will create a simple form for input where you can upload a CSV file and then will put a button to export users as excel data from the database in an excel file.
So, let's start the below step to create the import and export function in laravel 9 application. you can export file with .csv, .xls and .xlsx file.
Step 1: Install Laravel 9
We are going to start from scratch. So download a fresh Laravel application like:
composer create-project laravel/laravel example-app
Step 2: Install maatwebsite/excel Package
In this step, we have to install maatwebsite/excel
package via the Composer package manager, so one your terminal and fire bellow command:
composer require psr/simple-cache:^1.0 maatwebsite/excel
If you are running less than the laravel 9 versions then use bellow command:
composer require maatwebsite/excel
Recommended : Laravel 9 Upload CSV File Example Without Packages
Step 3: Create Dummy Records
In this third step, I will create some dummy records for the users' table, so we can export them with that users. so let's run bellow command:
php artisan tinker
User::factory()->count(10)->create()
Step 4: Create Import Class
In maatwebsite
3 versions provide a way to build an import class that we have to use 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
php artisan make:import UsersImport --model=User
Now it will generate and below file and update it like:
app/Imports/UsersImport.php
namespace App\Imports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Hash;
class UsersImport implements ToModel, WithHeadingRow
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new User([
'name' => $row['name'],
'email' => $row['email'],
'password' => Hash::make($row['password']),
]);
}
}
Step 5: Create Export Class
In the maatwebsite
3 versions provide 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:
php artisan make:export UsersExport --model=User
Now it will generate and below file and update it like:
app/Exports/UsersExport.php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class UsersExport implements FromCollection, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::select("id", "name", "email")->get();
}
/**
* Write code on Method
*
* @return response()
*/
public function headings(): array
{
return ["ID", "Name", "Email"];
}
}
Step 6: Create Controller
In this step, we will create UserController with index(), export() and import() method. so first let's create a controller by following the command and updating code on it.
php artisan make:controller UserController
Now, update the code on the UserController file.
app/Http/Controllers/UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exports\UsersExport;
use App\Imports\UsersImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\User;
class UserController extends Controller
{
/**
* @return \Illuminate\Support\Collection
*/
public function index()
{
$users = User::get();
return view('users', compact('users'));
}
/**
* @return \Illuminate\Support\Collection
*/
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
/**
* @return \Illuminate\Support\Collection
*/
public function import()
{
Excel::import(new UsersImport,request()->file('file'));
return back();
}
}
Step 7: Create Routes
In this step, we need to create routes for a list of users, import users, and export users. so open your "routes/web.php" file and add the following route.
routes/web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| 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::controller(UserController::class)->group(function(){
Route::get('users', 'index');
Route::get('users-export', 'export')->name('users.export');
Route::post('users-import', 'import')->name('users.import');
});
Step 8: Create Blade File
In the last step, let's create users.blade.php(resources/views/users.blade.php) for layout and we will write design code here and put the following code:
resources/views/users.blade.php
Read also: Upload Large CSV File using Queue Job Batching in Laravel
Hope this Laravel 9 export excel and csv upload tutorial will help you.
#laravel #laravel-9x #maatwebsiteexcel #packages