admin管理员组

文章数量:1291109

I have a many-to-many relationship between roles and permissions in my application. I already have the pivot table with columns role_id, permission_id, and a new column access_type with an enum that contains the values all_access and view_only.

Here’s a simple example of how I am using Filament’s pivotData to pass data to the pivot table:

use Filament\Forms\Components\Select;

Select::make('primaryTechnologies')
    ->relationship(name: 'technologies', titleAttribute: 'name')
    ->multiple()
    ->pivotData([
        'is_primary' => true,
    ]);

I want to achieve the same thing for the access_type column, where I can manually select the enum value (all_access or view_only) for each relationship between roles and permissions. How can I set up pivotData to allow for this selection in the Filament form?

Any help or guidance would be greatly appreciated!

I tried using the pivotData approach to set the default value for access_type in a CheckboxList, like this:

Forms\Components\CheckboxList::make('role_permissions')
                            ->relationship('permissions', 'name')
                            ->pivotData([
                                'access_type' => 'all_access',
                            ])

Role Model

public function permissions()
{
    return $this->belongsToMany(Permission::class, 'role_permissions')
        ->withPivot('access_type')
        ->withTimestamps();
}

Permission Model

public function roles()
{
    return $this->belongsToMany(Role::class, 'role_permissions')
        ->withPivot('access_type')
        ->withTimestamps();
}

Roles Migration

public function up(): void
{
    Schema::create('roles', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->softDeletes();
        $table->string('name');
        $table->string('slug')->nullable();
        $table->string('scope')->nullable();
        $table->boolean('is_protected')->default(0);
    });
}

Permission Migration

public function up(): void
{
    Schema::create('permissions', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->softDeletes();
        $table->string('name')->unique();
    });
}

Role_Permissions Migration

public function up(): void
{
    Schema::create('role_permissions', function (Blueprint $table) {
        $table->id();
        $table->timestamps();
        $table->softDeletes();
        $table->foreignId('role_id')->nullable()->constrained('roles')->cascadeOnDelete();
        $table->foreignId('permission_id')->nullable()->constrained('permissions')->cascadeOnDelete();
        $table->enum('access_type', ['all_access', 'view_only'])->nullable();
    });
}

This results in the access_type being set to all_access for all selected permissions in the pivot table, which is not what I want. I need to allow the user to select between all_access and view_only for each permission.

本文标签: laravelHow to use pivotData with enum column in a Filament 3 manytomany relationshipStack Overflow