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 and adding to the doc: Field team_id doesn't have a default value #138

Open
juleindev opened this issue Dec 19, 2023 · 11 comments
Open
Labels
documentation Improvements or additions to documentation

Comments

@juleindev
Copy link

juleindev commented Dec 19, 2023

Screenshot 2023-12-18 171358

Could you add this to your documentation for the tenant section?

Forms\Components\Select::make('roles')
            ->relationship(name: 'roles', titleAttribute: 'name')
            ->saveRelationshipsUsing(function (Model $record, $state) {
                 $record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => getPermissionsTeamId()]);
            })
           ->multiple()
           ->preload()
           ->searchable(),
@juleindev juleindev changed the title Field team_id doesn't have a default value Issue and doc add: Field team_id doesn't have a default value Dec 19, 2023
@juleindev juleindev changed the title Issue and doc add: Field team_id doesn't have a default value Issue and adding to the doc: Field team_id doesn't have a default value Dec 19, 2023
@CodeTechNL
Copy link
Contributor

@juleindev which file is this? Running in this problem now

@juleindev
Copy link
Author

@CodeTechNL

In the Form of your UserResource file for example

@CodeTechNL
Copy link
Contributor

@juleindev I also get it on the roles resource page. Did you found a fix for that?

@juleindev
Copy link
Author

@CodeTechNL

I didn't had any issue with the package in the Role resource, if you were follow the Tenant Section of the documentation.
I think there is a missing relationship in one of yours models at first sight.

Can you put a screenshot of the error message?

@CodeTechNL
Copy link
Contributor

CodeTechNL commented Dec 22, 2023 via email

@Edsardio
Copy link

Edsardio commented Feb 2, 2024

@CodeTechNL did you ever solve the issue?

@irvingviveros
Copy link

@Edsardio I am also facing this issue, but I think is related to this other issue (which the user resolved adding Filament::getTenant()->id as a parameter value)

#139

@Edsardio
Copy link

Edsardio commented Feb 5, 2024

@irvingviveros Hmm, I've got the issue when attaching a user to the role, which happens automatically on creating a role.. Basically making the package unusable

@tharindarodrigo
Copy link
Contributor

The Filament::getTenant()->id issue has been fixed

@tharindarodrigo tharindarodrigo added the documentation Improvements or additions to documentation label Feb 5, 2024
@irvingviveros
Copy link

irvingviveros commented Feb 6, 2024

@irvingviveros Hmm, I've got the issue when attaching a user to the role, which happens automatically on creating a role.. Basically making the package unusable

Ok now I am facing the same issue.

I can attach a role when I am creating a user, but when I try to attach a Role to the User Resource with RolesRelationManager, I got the same error (1364 Field 'team_id' doesn't have a default value)

image

class RolesRelationManager extends RelationManager
{
    protected static string $relationship = 'roles';

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->required()
                    ->maxLength(255)
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('name')
            ->columns([
                Tables\Columns\TextColumn::make('name'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\AttachAction::make()
                    ->preloadRecordSelect()
            ])
            ->actions([
                Tables\Actions\DetachAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DetachBulkAction::make(),
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}

I've been researching this error for several days but I can't find the answer. It also happens with the RoleResource and the UserRelationManager generated by the package, I cannot attach users to the role on the edit page because the same problem occurs.

I have already tried installing the plugin twice in a clean installation but I can't get it to run correctly. Perhaps more information is needed on this or it is a bit ambiguous. I appreciate the efforts the team behind the plugin put into maintaining it.

@Cedric331
Copy link

@irvingviveros

You must create an extend the Spatie Role model and override by adding "withPivotValue":

`namespace App\Models;

use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Spatie\Permission\Models\Role as RoleSpatie;
use Spatie\Permission\PermissionRegistrar;

class Role extends RoleSpatie
{
use HasFactory;

public function users(): BelongsToMany
{
    return $this->morphedByMany(
        getModelForGuard($this->attributes['guard_name'] ?? config('auth.defaults.guard')),
        'model',
        config('permission.table_names.model_has_roles'),
        app(PermissionRegistrar::class)->pivotRole,
        config('permission.column_names.model_morph_key')
    )->withPivotValue('shop_id', Filament::getTenant()->id);
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

6 participants