Skip to content

Commit

Permalink
feat: withExportable
Browse files Browse the repository at this point in the history
  • Loading branch information
pxlrbt committed Feb 19, 2022
1 parent 5971ed0 commit 1aa23f5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ composer install pxlrbt/filament-excel

## Usage

### Quick start
Go to your Filament resource and add the `ExportAction` to the tables bulk actions:

```php
Expand All @@ -38,6 +39,7 @@ class User extends Resource
}
}
```
### Options

Optionally configure your export:

Expand All @@ -57,6 +59,39 @@ Optionally configure your export:
]);
```

### Custom exports

If you need even more customization you can use custom export class:

```php
ExportAction::make('export')
->label('Export Data')
->withExportable(Export::class)
```

Important data will be injected into the constructor automatically:

```php
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;

class Export implements FromCollection
{
public function __construct($records, $model, $livewire, $action)
{
$this->records = $records;
}

public function collection()
{
return $this->records;
}
}
```


## Contributing

Expand Down
1 change: 1 addition & 0 deletions src/Actions/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected function export(Collection $records, array $data)
$this->handleFilename($data);
$this->handleWriterType($data);


return Excel::download(
$this->getExportable(),
$this->getFilename(),
Expand Down
11 changes: 9 additions & 2 deletions src/Concerns/WithExportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait WithExportable
{
protected object $exportable;
protected ?string $exportable = null;

public function withExportable($exportable): self
{
Expand All @@ -15,6 +15,13 @@ public function withExportable($exportable): self

public function getExportable(): object
{
return $this->exportable ?? $this;
return $this->exportable === null
? $this
: app($this->exportable, [
'action' => $this->action,
'resource' => $this->resource,
'model' => $this->model,
'records' => $this->records,
]);
}
}

0 comments on commit 1aa23f5

Please sign in to comment.