Skip to content

Commit

Permalink
Merge pull request #10024 from filamentphp/fix/import-user
Browse files Browse the repository at this point in the history
fix: Import user
  • Loading branch information
danharrin authored Dec 2, 2023
2 parents 33778a8 + ae1d8ba commit fb2641d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
39 changes: 39 additions & 0 deletions packages/actions/docs/07-prebuilt-actions/08-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,45 @@ ImportColumn::make('sku')
->example('ABC123')
```

## Using a custom user model

By default, the `imports` table has a `user_id` column. That column is constrained to the `users` table:

```php
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
```

In the `Import` model, the `user()` relationship is defined as a `BelongsTo` relationship to the `App\Models\User` model. If the `App\Models\User` model does not exist, or you want to use a different one, you can bind a new `Authenticatable` model to the container in a service provider's `register()` method:

```php
use App\Models\Admin;
use Illuminate\Contracts\Auth\Authenticatable;

$this->app->bind(Authenticatable::class, Admin::class);
```

If your authenticatable model uses a different table to `users`, you should pass that table name to `constrained()`:

```php
$table->foreignId('user_id')->constrained('admins')->cascadeOnDelete();
```

### Using a polymorphic user relationship

If you want to associate imports with multiple user models, you can use a polymorphic `MorphTo` relationship instead. To do this, you need to replace the `user_id` column in the `imports` table:

```php
$table->morphs('user');
```

Then, in a service provider's `boot()` method, you should call `Import::polymorphicUserRelationship()` to swap the `user()` relationship on the `Import` model to a `MorphTo` relationship:

```php
use Filament\Actions\Imports\Models\Import;

Import::polymorphicUserRelationship();
```

## Limiting the maximum number of rows that can be imported

To prevent server overload, you may wish to limit the maximum number of rows that can be imported from one CSV file. You can do this by calling the `maxRows()` method on the action:
Expand Down
2 changes: 1 addition & 1 deletion packages/actions/resources/lang/zh_CN/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@
'system_error' => '系统错误,请联系支持。',
],

]
],
];
29 changes: 28 additions & 1 deletion packages/actions/src/Imports/Models/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Actions\Imports\Models;

use Carbon\CarbonInterface;
use Exception;
use Filament\Actions\Imports\Importer;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -33,14 +34,30 @@ class Import extends Model

protected $guarded = [];

protected static bool $hasPolymorphicUserRelationship = false;

public function failedRows(): HasMany
{
return $this->hasMany(app(FailedImportRow::class)::class);
}

public function user(): BelongsTo
{
return $this->belongsTo(app(Authenticatable::class)::class);
if (static::hasPolymorphicUserRelationship()) {
return $this->morphTo();
}

$authenticatable = app(Authenticatable::class);

if ($authenticatable) {
return $this->belongsTo($authenticatable::class);
}

if (! class_exists(\App\Models\User::class)) {
throw new Exception('No [App\\Models\\User] model found. Please bind an authenticatable model to the [Illuminate\\Contracts\\Auth\\Authenticatable] interface in a service provider\'s [register()] method.');
}

return $this->belongsTo(\App\Models\User::class);
}

/**
Expand All @@ -62,4 +79,14 @@ public function getFailedRowsCount(): int
{
return $this->total_rows - $this->successful_rows;
}

public static function polymorphicUserRelationship(bool $condition = true): void
{
static::$hasPolymorphicUserRelationship = $condition;
}

public static function hasPolymorphicUserRelationship(): bool
{
return static::$hasPolymorphicUserRelationship;
}
}
3 changes: 1 addition & 2 deletions packages/forms/resources/lang/fi/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
'no_fixed' => [
'label' => 'Vapaa',
],

],

'svg' => [
Expand Down Expand Up @@ -250,7 +250,6 @@

],


'radio' => [

'boolean' => [
Expand Down

0 comments on commit fb2641d

Please sign in to comment.