Repeater MorphToMany

Hi everyone! I'm having a rough time with managing a MorphToMany relationship with a repeater. I'm currently managing permissions, roles and users. Model/Permission.php
public function roles(): MorphToMany
{
return $this->morphedByMany(Role::class, 'grantable');
}

public function users(): MorphToMany
{
return $this->morphedByMany(User::class, 'grantable');
}
public function roles(): MorphToMany
{
return $this->morphedByMany(Role::class, 'grantable');
}

public function users(): MorphToMany
{
return $this->morphedByMany(User::class, 'grantable');
}
Model/User.php:
public function permissions(): MorphToMany
{
return $this->morphToMany(Permission::class, 'grantable');
}
public function permissions(): MorphToMany
{
return $this->morphToMany(Permission::class, 'grantable');
}
Model/Role.php:
public function permissions(): MorphToMany
{
return $this->morphToMany(Permission::class, 'grantable');
}
public function permissions(): MorphToMany
{
return $this->morphToMany(Permission::class, 'grantable');
}
Logic works fine, except the Repeater. I followed this documentation: https://filamentphp.com/docs/3.x/forms/fields/repeater#integrating-with-a-belongstomany-eloquent-relationship and created the pivot class:
class Grantable extends Pivot
{
use HasFactory;

protected $table = 'grantables';

public function permission(): BelongsTo
{
return $this->belongsTo(Permission::class);
}

public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}
}
class Grantable extends Pivot
{
use HasFactory;

protected $table = 'grantables';

public function permission(): BelongsTo
{
return $this->belongsTo(Permission::class);
}

public function role(): BelongsTo
{
return $this->belongsTo(Role::class);
}
}
and in Model/Permission.php:
public function grantables(): HasMany
{
return $this->hasMany(Grantable::class);
}
public function grantables(): HasMany
{
return $this->hasMany(Grantable::class);
}
1 Reply
Davide Cariola
Davide CariolaOP14mo ago
Then in my PermissionResource I have:
Section::make()
->schema([
Repeater::make('grantables')
->relationship()
->simple(
Select::make('grantable_id')
->relationship(
'role',
'name',
fn(Builder $query) => $query->orderBy('order'),
),
)
->deleteAction(
fn (Action $action) => $action->requiresConfirmation(),
)
->addActionLabel('Assegna a un altro ruolo')
->reorderable(false)
->label('Scegli un ruolo')
->grid(2)
])
->heading('A quali ruoli lo vuoi assegnare?')
->hiddenOn('edit'),
Section::make()
->schema([
Repeater::make('grantables')
->relationship()
->simple(
Select::make('grantable_id')
->relationship(
'role',
'name',
fn(Builder $query) => $query->orderBy('order'),
),
)
->deleteAction(
fn (Action $action) => $action->requiresConfirmation(),
)
->addActionLabel('Assegna a un altro ruolo')
->reorderable(false)
->label('Scegli un ruolo')
->grid(2)
])
->heading('A quali ruoli lo vuoi assegnare?')
->hiddenOn('edit'),
But when I try to save I get this error: SQLSTATE[HY000]: General error: 1364 Field 'grantable_type' doesn't have a default value which I think it means that it cannot manage the grantable_type column with the path to the model. Any idea how to solve? Thanks everyone
Want results from more Discord servers?
Add your server