From 60abe97ab0b40cdf0cafb75e518b0f08ee1b7724 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sat, 2 Dec 2023 22:08:36 +0000 Subject: [PATCH 1/3] fix: Import user --- .../docs/07-prebuilt-actions/08-import.md | 39 +++++++++++++++++++ .../actions/src/Imports/Models/Import.php | 29 +++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/packages/actions/docs/07-prebuilt-actions/08-import.md b/packages/actions/docs/07-prebuilt-actions/08-import.md index 909de1238b5..6071ffcbca9 100644 --- a/packages/actions/docs/07-prebuilt-actions/08-import.md +++ b/packages/actions/docs/07-prebuilt-actions/08-import.md @@ -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: diff --git a/packages/actions/src/Imports/Models/Import.php b/packages/actions/src/Imports/Models/Import.php index 98b927184ea..55c6ad3927a 100644 --- a/packages/actions/src/Imports/Models/Import.php +++ b/packages/actions/src/Imports/Models/Import.php @@ -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 @@ public function failedRows(): HasMany 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 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 @@ 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; + } } From 989703118e77623d80184e82aeed46245d848186 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sat, 2 Dec 2023 22:10:15 +0000 Subject: [PATCH 2/3] Update Import.php --- packages/actions/src/Imports/Models/Import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/actions/src/Imports/Models/Import.php b/packages/actions/src/Imports/Models/Import.php index 55c6ad3927a..e3f84c40c7c 100644 --- a/packages/actions/src/Imports/Models/Import.php +++ b/packages/actions/src/Imports/Models/Import.php @@ -54,7 +54,7 @@ public function user(): BelongsTo } if (! class_exists(\App\Models\User::class)) { - throw new Exception('No user model found. Please bind an authenticatable model to the [Illuminate\\Contracts\\Auth\\Authenticatable] interface in a service provider\'s [register()] method.'); + 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); From ae1d8bac26e2f4f35fec32323109c6e8608773ea Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sat, 2 Dec 2023 22:11:17 +0000 Subject: [PATCH 3/3] wip --- packages/actions/resources/lang/zh_CN/import.php | 2 +- packages/forms/resources/lang/fi/components.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/actions/resources/lang/zh_CN/import.php b/packages/actions/resources/lang/zh_CN/import.php index 742d1486d26..01384d51bcf 100644 --- a/packages/actions/resources/lang/zh_CN/import.php +++ b/packages/actions/resources/lang/zh_CN/import.php @@ -71,5 +71,5 @@ 'system_error' => '系统错误,请联系支持。', ], - ] + ], ]; diff --git a/packages/forms/resources/lang/fi/components.php b/packages/forms/resources/lang/fi/components.php index 8e1a9401708..26607861ac0 100644 --- a/packages/forms/resources/lang/fi/components.php +++ b/packages/forms/resources/lang/fi/components.php @@ -182,7 +182,7 @@ 'no_fixed' => [ 'label' => 'Vapaa', ], - + ], 'svg' => [ @@ -250,7 +250,6 @@ ], - 'radio' => [ 'boolean' => [