Merge pull request #10024 from filamentphp/fix/import-user

fix: Import user
This commit is contained in:
Dan Harrin
2023-12-02 22:11:27 +00:00
committed by GitHub
4 changed files with 69 additions and 4 deletions
@@ -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:
@@ -71,5 +71,5 @@ return [
'system_error' => '系统错误,请联系支持。',
],
]
],
];
+28 -1
View File
@@ -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;
@@ -33,6 +34,8 @@ class Import extends Model
protected $guarded = [];
protected static bool $hasPolymorphicUserRelationship = false;
public function failedRows(): HasMany
{
return $this->hasMany(app(FailedImportRow::class)::class);
@@ -40,7 +43,21 @@ class Import extends Model
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);
}
/**
@@ -62,4 +79,14 @@ class Import extends Model
{
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;
}
}
@@ -182,7 +182,7 @@ return [
'no_fixed' => [
'label' => 'Vapaa',
],
],
'svg' => [
@@ -250,7 +250,6 @@ return [
],
'radio' => [
'boolean' => [