In this lesson, we will use Laravel Excel package to export selected products into CSV, XLSX, and PDF.
Let's start by adding three buttons, which will trigger the export()
method in the Livewire component.
resources/views/livewire/products-list.blade.php:
<div class="mb-4"> <div class="mb-4"> <a class="inline-flex items-center px-4 py-2 text-xs font-semibold tracking-widest text-white uppercase bg-gray-800 rounded-md border border-transparent hover:bg-gray-700"> Create Product </a> </div> <button type="button" wire:click="deleteConfirm('deleteSelected')" wire:loading.attr="disabled" @disabled(! $this->selectedCount) class="px-4 py-2 mr-5 text-xs text-red-500 uppercase bg-red-200 rounded-md border border-transparent hover:text-red-700 hover:bg-red-300 disabled:opacity-50 disabled:cursor-not-allowed"> Delete Selected </button> <x-primary-button wire:click="export('csv')">CSV</x-primary-button> <x-primary-button wire:click="export('xlsx')">XLSX</x-primary-button> <x-primary-button wire:click="export('pdf')">PDF</x-primary-button> </div>
Next, we need to install the Laravel Excel package and create Export.
composer require maatwebsite/excel dompdf/dompdfphp artisan make:export ProductsExport --model=Product
Dompdf is needed for exporting to PDF.
Now what everything means inside ProductExport
you can read in the official documentation.
app/Exports/ProductsExport.php:
use App\Models\Product;use Illuminate\Support\Collection;use Maatwebsite\Excel\Concerns\WithMapping;use Maatwebsite\Excel\Concerns\WithHeadings;use Maatwebsite\Excel\Concerns\FromCollection; class ProductsExport implements FromCollection, WithHeadings, WithMapping{ public function __construct(private array $productIDs) {} public function headings(): array { return [ 'Name', 'Categories', 'Country', 'Price' ]; } public function map($product): array { return [ $product->name, $product->categories->pluck('name')->implode(', '), $product->country->name, '$' . number_format($product->price, 2) ]; } public function collection(): Collection { return Product::with('categories', 'country')->find($this->productIDs); }}
All that's left is to implement the export()
method in the LIvewire Component.
app/Livewire/ProductsList.php:
use App\Exports\ProductsExport;use Maatwebsite\Excel\Facades\Excel;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpFoundation\BinaryFileResponse; class ProductsList extends Component{ // ... public function export(string $format): BinaryFileResponse { abort_if(! in_array($format, ['csv', 'xlsx', 'pdf']), Response::HTTP_NOT_FOUND); return Excel::download(new ProductsExport($this->selected), 'products.' . $format); } // ...}
Now, after selecting some products and clicking one of the buttons to export, you will get exported file.
Hey Povilas, I try to export with every method but data need to be selected. if not selected the export file is empy. How to export all data withoud be selected? Can you add a button to select everthing on the page? Can you show production quantity? For example I want to click a button/select to show 100 or 50 or 1000 products. At least can you create that as components on livewire kit?
A few ideas for the future, thanks. For now I'm waiting for Livewire 3 and then will create more content about Livewire, and refresh the old ones.
Can you add dark mode switcher, infinity loading on livewire kit? On bulk delete add the select all checkbox. Its great features. I start using the livewire and the livewire kit is very helpful. I love livewire kit and I wait the new version with livewire 3.
We'll see about new components for Livewire Kit when Livewire 3 is released, thanks for the ideas.
@prpanto dark mode switcher shouldn't be made with Livewire. You should use Alpine.js for that.
Hello, I don't know if it is the best solution, but to export all without select any register I did this
And it's worked like a charm!
Hello,
Should the command : php artisan make:export ProductsExport --model=Product
Create the complete ProductsExport as in your tutorial. Because in my case I only have :
class ProductsExport implements FromCollection { /** * @return \Illuminate\Support\Collection */ public function collection() { return Product::all(); } }
No, the command itself doesn't write the full code for you, you have to write it yourself.
When I run php artisan make:export ProductsExport --model=Product I am getting a error
ERROR Command "make:export" is not defined. Did you mean one of these?
⇂ make:cast
⇂ make:channel ⇂ make:command ⇂ make:component ⇂ make:controller ⇂ make:event ⇂ make:exception ⇂ make:factory ⇂ make:job ⇂ make:request ⇂ make:resource ⇂ make:rule ⇂ make:scope ⇂ make:seeder ⇂ make:test I am running PhpStorm 2023.1, Laravel 10.9.0, breeze 1.20.2, Livewire 2.12.3 and maatwebsite/excel 1.1.5 ran composer update and I got Nothing to install, update or remove. WHAT am I missing why am I getting this ERROR ???
Ps. I did run your composer require maatwebsite/excel dompdf/dompdf First then php artisan make:export ProductsExport --model=Product
When running: composer require maatwebsite/excel dompdf/dompdf
I got all kind of errors in the output:
Also php artisan make:export ProductsExport --model=Product didn't work at all:
This part of the class doesn't work for me at all using Laravel 10 on windows 10
It seems that something is wrong with the package maybe. Somehow it tries to install wrong version. To fix it now manually add
"maatwebsite/excel": "^3.1"
to thecomposer.json
and runcomposer update
.Hi RangeCoder did you solve that ?
Look like I did Syed, I had to uncomment extension=zip in my PHP.ini settings file
After doing what Nerijus suggested I got this error (THANK YOU Nerijus!):
With a wall of text after it but it had one useful thing in the end...
So after that I typed: composer update --with-all-dependencies and got this error:
That was much more useful like I pointed at that ext-zip was requered. So enabled that ext in the php settings and retyping that command it worked!
Also php artisan make:export ProductsExport --model=Product did work after!
It's something with that excel package that it won't install the correct version. I think it would be best if you would raise a issue to the author.
If you have problems installing, you can try this way
When I run
php artisan make:export ProductsExport --model=Product
I am getting a error
ERROR Command "make:export" is not defined. Did you mean one of these?
⇂ make:cast ⇂ make:channel ⇂ make:command ⇂ make:component ⇂ make:controller ⇂ make:event ⇂ make:exception ⇂ make:factory ⇂ make:job ⇂ make:request ⇂ make:resource ⇂ make:rule ⇂ make:scope ⇂ make:seeder ⇂ make:test
I am running PhpStorm 2023.1, Laravel 10.9.0, breeze 1.20.2, Livewire 2.12.3 and maatwebsite/excel 1.1.5 with psr/simple-cache 1.0.0 ran composer update and I got Nothing to install, update or remove. WHAT am I missing why am I getting this ERROR ???
Ps. I did run your composer require maatwebsite/excel dompdf/dompdf First then php artisan make:export ProductsExport --model=Product
composer require maatwebsite/excel
installs by default the version1.0
and its outdated. You need install a version^3.1
. Therefore you must run the commandHears is what worked for me
First in the Php ini file enable gd and zip to make this work. Nest install composer require phpoffice/phpspreadsheet -W Then composer require maatwebsite/excel dompdf/dompdf Next php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config And Finaly php artisan make:export ProductsExport --model=Product CSV XLSX AND PDF all worked just fine for me.
The last step of this part of the tutorial showing how to add the export() method in the ProductsList component shows this use statement;
I had to also use the following classes to get the file exports to work;
For the response namespace I believe it should be from symfony.
Ah, thanks - believe it or not I'd done the whole course up to this point without realising there was a repo for it on Github. D'oh. I think I realised in the next lesson. You are spot on.