At Maatwebsite we love Laravel! Of course we couldn’t wait to get our hands on Laravel Nova! We can tell you… the wait was worth it! Considering our primary open source focus is Laravel Excel, we couldn’t stay behind and now introduce you Laravel Nova Excel.
Laravel Nova Excel integrates Laravel Nova resources and Laravel Excel seamlessly. Integration is as easy as adding one class to your list of resource actions!
Before integrating this package, make sure you have Laravel Nova installed. Then install maatwebsite/laravel-nova-excel
via composer. This will download the package and Laravel-Excel.
composer require maatwebsite/laravel-nova-excel
Go to your Nova resource. As example we’ll use the app/Nova/User.php
. Add Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel
action to your actions()
list.
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel;
class User extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\\User';
// Other default resource methods
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
*
* @return array
*/
public function actions(Request $request)
{
return [
new DownloadExcel(),
];
}
}
Go to your resource in your Nova admin panel, select all or some users and click “Download Excel”.
In your downloads folder you will find your users.xlsx! The name of the file is auto-generated based on the plural form of the resource name. But of course this is fully configurable!
public function actions(Request $request)
{
return [
(new DownloadExcel())->withFilename('users-2018.xlsx'),
];
}
Prefer your customer to choose the filename themselves? No problem! Use the askForFilename
method to prompt a question after clicking “Download Excel”.
public function actions(Request $request)
{
return [
(new DownloadExcel())->askForFilename(),
];
}
As Laravel Nova Excel delegates all the heavy-lifting to Laravel Excel, it’s also possible to customize your entire export.
You can read more about it in our dedicated documentation: https://laravel-excel.maatwebsite.nl/nova/1.0/getting-started/
Originally posted at: https://medium.com/maatwebsite/introducing-laravel-nova-excel-b7b9a0bb0f7d
I work at Maatwebsite on weekdays and I am a passionate filmmaker in my free time. Find me on Twitter & Github.