Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with assigning user roles on team #203

Open
jimiero opened this issue Sep 27, 2024 · 0 comments
Open

Issue with assigning user roles on team #203

jimiero opened this issue Sep 27, 2024 · 0 comments

Comments

@jimiero
Copy link

jimiero commented Sep 27, 2024

Hello,

I'm using your package, when I first time edit it assigns correctly the role, but when I return again to edit page, and click again Save button I get this error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1-236-App\Models\User' for key 'PRIMARY' (Connection: mysql, SQL: insert into model_has_roles (model_id, model_type, role_id, team_id`) values (236, App\Models\User, 1, 1))

`

This is the code I use:

Forms\Components\Select::make('roles')
                                ->relationship(name: 'roles', titleAttribute: 'name')
                                ->saveRelationshipsUsing(function ($record, $state, $get) {
                                    $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => 1]); // I'm hardcoding the team id for now
                                })
                                ->multiple()
                                ->preload()
                                ->searchable()```

Any idea what I'm doing wrong?

This is the migration code from spatie permissions for mode_has_roles:

```php 
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
            $table->unsignedBigInteger($pivotRole);

            $table->string('model_type');
            $table->unsignedBigInteger($columnNames['model_morph_key']);
            $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');

            $table->foreign($pivotRole)
                ->references('id') // role id
                ->on($tableNames['roles'])
                ->onDelete('cascade');
            if ($teams) {
                $table->unsignedBigInteger($columnNames['team_foreign_key']);
                $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');

                $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
                    'model_has_roles_role_model_type_primary');
            } else {
                $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
                    'model_has_roles_role_model_type_primary');
            }
        });`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant