Skip to content

Commit

Permalink
Merge pull request #10325 from tonypartridge/patch-4
Browse files Browse the repository at this point in the history
Add relationManager explanation, and confirm import action since Filamentphp 3.1
  • Loading branch information
danharrin authored Dec 15, 2023
2 parents 5ded539 + 202e882 commit 1fd3b96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/actions/docs/07-prebuilt-actions/08-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Import action

## Overview

Filament includes a prebuilt action that is able to import rows from a CSV. When the trigger button is clicked, a modal asks the user for a file. Once they upload one, they are able to map each column in the CSV to a real column in the database. If any rows fail validation, they will be compiled into a downloadable CSV for the user to review after the rest of the rows have been imported. Users can also download an example CSV file containing all the columns that can be imported.
Filament v3.1 introduced a prebuilt action that is able to import rows from a CSV. When the trigger button is clicked, a modal asks the user for a file. Once they upload one, they are able to map each column in the CSV to a real column in the database. If any rows fail validation, they will be compiled into a downloadable CSV for the user to review after the rest of the rows have been imported. Users can also download an example CSV file containing all the columns that can be imported.

This feature uses [job batches](https://laravel.com/docs/queues#job-batching) and [database notifications](../../notifications/database-notifications#overview), so you need to publish those migrations from Laravel. Also, you need to publish the migrations for tables that Filament uses to store information about imports:

Expand Down Expand Up @@ -387,20 +387,32 @@ use Filament\Forms\Components\Checkbox;
public static function getOptionsFormComponents(): array
{
return [
Checkbox::make('update_existing')
Checkbox::make('updateExisting')
->label('Update existing records'),
];
}
```

Alternatively, you can pass a set of static options to the importer through the `options()` method on the action:

```php
use Filament\Actions\ImportAction;

ImportAction::make()
->importer(ProductImporter::class)
->options([
'updateExisting' => true,
])
```

Now, you can access the data from these options inside the importer class, by calling `$this->options`. For example, you might want to use it inside `resolveRecord()` to [update an existing product](#updating-existing-records-when-importing):

```php
use App\Models\Product;

public function resolveRecord(): ?Product
{
if ($this->options['update_existing'] ?? false) {
if ($this->options['updateExisting'] ?? false) {
return Product::firstOrNew([
'sku' => $this->data['sku'],
]);
Expand Down
34 changes: 34 additions & 0 deletions packages/panels/docs/03-resources/07-relation-managers.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,40 @@ public function table(Table $table): Table

To learn how to customize the `DeleteAction`, including changing the notification and adding lifecycle hooks, please see the [Actions documentation](../../actions/prebuilt-actions/delete).

## Importing related records

The [`ImportAction`](../../actions/prebuilt-actions/import) can be added to the header of a relation manager to import records. In this case, you probably want to tell the importer which owner these new records belong to. You can use [import options](../../actions/prebuilt-actions/import#using-import-options) to pass through the ID of the owner record:

```php
ImportAction::make()
->importer(ProductImporter::class)
->options(['categoryId' => $this->getOwnerRecord()->getKey()])
```

Now, in the importer class, you can associate the owner in a one-to-many relationship with the imported record:

```php
public function resolveRecord(): ?Product
{
$product = Product::firstOrNew([
'sku' => $this->data['sku'],
]);

$product->category()->associate($this->options['categoryId']);

return $product;
}
```

Alternatively, you can attach the record in a many-to-many relationship using the `afterSave()` hook of the importer:

```php
protected function afterSave(): void
{
$this->record->categories()->syncWithoutDetaching([$this->options['categoryId']]);
}
```

## Accessing the relationship's owner record

Relation managers are Livewire components. When they are first loaded, the owner record (the Eloquent record which serves as a parent - the main resource model) is saved into a property. You can read this property using:
Expand Down

0 comments on commit 1fd3b96

Please sign in to comment.