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');
}


Model/User.php:
public function permissions(): MorphToMany
{
    return $this->morphToMany(Permission::class, 'grantable');
}


Model/Role.php:
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); 
    }
}


and in Model/Permission.php:
public function grantables(): HasMany
{
    return $this->hasMany(Grantable::class);
}
Was this page helpful?