think.studio/nova-resource-dynamic-export

Functionality to dynamically export resources.

1.0.0 2023-09-06 05:20 UTC

This package is auto-updated.

Last update: 2024-11-06 07:50:50 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

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

Credits

  • Think Studio