Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shahr's avatar
Level 10

How to export exel Laravel?

I incstalled maatwebsite/excel package. I want to export Excel into Laravel. How do I do it?

0 likes
3 replies
LaryAI's avatar
Level 58

To export Excel in Laravel using the maatwebsite/excel package, you can follow these steps:

  1. Install the package using Composer:
composer require maatwebsite/excel
  1. Publish the configuration file:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
  1. Create a new export class using the make:export Artisan command:
php artisan make:export UsersExport --model=User
  1. In the UsersExport class, define the data that you want to export in the query() method:
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromQuery;

class UsersExport implements FromQuery
{
    public function query()
    {
        return User::query();
    }
}
  1. In your controller, create a new instance of the UsersExport class and use the download() method to generate and download the Excel file:
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends Controller
{
    public function export()
    {
        return Excel::download(new UsersExport, 'users.xlsx');
    }
}
  1. Finally, create a route to your controller method:
Route::get('/export', [UserController::class, 'export']);

Now you can visit /export in your browser to download the Excel file.

Please or to participate in this conversation.