How to show / edit pivot attributes in a relation manager

I have two models
  • Post Model
  • Comment Model
    Each of them has belongsToMany() relation
    Note: Attaching and detaching are working fine
class Post extends Model
{
    
    //...

    public function Commects()
    {
        return $this->belongsToMany(Commect::class, 'post_commect')
            ->withPivot(['approved_at'])
            ->using('App\Models\PostCommect');
    }
}

class Commect extends Model
{

    //...

    public function Posts()
    {
        return $this->belongsToMany(Post:class, 'post_commect')
            ->withPivot(['approved_at'])
            ->using('App\Models\PostCommect');
    }
}

class PostCommect extends Pivot
{
    public function post()
    {
        return $this->belongsTo(Post:class, 'post_id');
    }

    public function commect()
    {
        return $this->belongsTo(Commect::class, 'commect_id');
    }
}

class CommentsRelationManager extends RelationManager
{
    protected static string $relationship = 'comments';

    protected static ?string $recordTitleAttribute = 'body';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('body'), //<----- Comment attribute
                Forms\Components\DatePicker::make('approved_at'), //<----- pivot attribute
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('body')->limit(50), //<----- Comment attribute (not showing)
                Tables\Columns\TextColumn::make('approved_at'), //<----- pivot attribute (not showing)
            ])
    }
}

When I add the edit action
Laravel gives me this error

Filament\Resources\RelationManagers\RelationManager::Filament\Resources\RelationManagers{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given

And is it possible to show pivot attributes along with model attattributes
Was this page helpful?