diff --git a/packages/actions/docs/07-prebuilt-actions/08-import.md b/packages/actions/docs/07-prebuilt-actions/08-import.md index 909de1238b..6071ffcbca 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/resources/lang/zh_CN/import.php b/packages/actions/resources/lang/zh_CN/import.php index 742d1486d2..01384d51bc 100644 --- a/packages/actions/resources/lang/zh_CN/import.php +++ b/packages/actions/resources/lang/zh_CN/import.php @@ -71,5 +71,5 @@ return [ 'system_error' => '系统错误,请联系支持。', ], - ] + ], ]; diff --git a/packages/actions/src/Imports/Models/Import.php b/packages/actions/src/Imports/Models/Import.php index 98b927184e..e3f84c40c7 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 @@ 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; + } } diff --git a/packages/forms/resources/lang/fi/components.php b/packages/forms/resources/lang/fi/components.php index 8e1a940170..26607861ac 100644 --- a/packages/forms/resources/lang/fi/components.php +++ b/packages/forms/resources/lang/fi/components.php @@ -182,7 +182,7 @@ return [ 'no_fixed' => [ 'label' => 'Vapaa', ], - + ], 'svg' => [ @@ -250,7 +250,6 @@ return [ ], - 'radio' => [ 'boolean' => [