think.studio / nova-resource-dynamic-export
Functionality to dynamically export resources.
1.0.0
2023-09-06 05:20 UTC
Requires
- php: ^8.1
- laravel/nova: ^4.0
- maatwebsite/laravel-nova-excel: ^1.3
- think.studio/laravel-json-field-cast: ^2.1
- think.studio/nova-html-field: ^2.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.21
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.13
README
Functionality to dynamically export resources.
Installation
You can install the package via composer:
composer require think.studio/nova-resource-dynamic-export # optional publish configs php artisan vendor:publish --provider="NovaResourceDynamicExport\ServiceProvider" --tag="config"
Update filesystem configuration if you will used default storage disk.
// config/filesystems.php 'exports' => [ 'driver' => 'local', 'root' => storage_path('app/exports'), ],
And add resource to your admin:
// Providers/NovaServiceProvider.php protected function resources(): void { parent::resources(); Nova::resources([ \NovaResourceDynamicExport\Nova\Resources\ExportStoredFile::class, ]); }
Please do not forget add policies for \NovaResourceDynamicExport\Models\ExportStoredFile model or your custom model
Usage
General resources export action
public function actions(NovaRequest $request): array { return [ \NovaResourceDynamicExport\Nova\Actions\ExportResourceAction::make() ->askForFilename() ->askForWriterType() ->askForColumns([ 'id', 'title' => 'Fund title', 'publication_status', 'description', 'color_code', 'selected_report', ]) ->setPostReplaceFieldValuesWhenOnResource(function ($array, \App\Models\Fund $model, $only) { if (in_array('selected_report', $only)) { $array['selected_report'] = $model->selectedReport->report_date?->format('Y-m-d'); } return $array; }), ]; }
Custom specified export
Firstly create custom export class
// Exports/PostsWithTagBreaking.php use Illuminate\Database\Eloquent\Builder; use Maatwebsite\Excel\Concerns\Exportable; use Maatwebsite\Excel\Concerns\FromQuery; use Maatwebsite\Excel\Concerns\WithHeadings; use Maatwebsite\Excel\Concerns\WithMapping; use NovaResourceDynamicExport\Export\CustomExport; use NovaResourceDynamicExport\Tests\Fixtures\Models\Post; class PostsWithTagBreaking extends CustomExport implements FromQuery, WithHeadings, WithMapping { use Exportable; public function query() { return Post::query() ->whereHas('tags', fn (Builder $q) => $q->where('name', 'Breaking')); } public function headings(): array { return [ 'Title', 'content', ]; } /** * @param Post $row */ public function map($row): array { return [ 'title' => $row->title, 'content' => $row->content, ]; } }
Then add this class using any service provider:
// Providers/NovaServiceProvider.php public function boot(): void { parent::boot(); CustomResourcesExport::use(PostsWithTagBreaking::class); }
THis is all, not in ExportStoredFile resource index you will see new action to run custom exports