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)
])
}
}
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
2 Replies
Filament
Filamentβ€’2y ago
We need more information to help you debug your problem. Please click on the top left 'SHARE' button of the error page you're seeing and share the link with us.
LancelotGamer
LancelotGamerOPβ€’2y ago
Sorry my bad 😦 I added the edit action to the headerActions I deleted the pivot model I deleted ->using('App\Models\PostCommect') from both model I checked the relation function name lol Post() it has to be post() posts* Comments() it has to be comments() Now everything works fine I used this to get the pivot attribute
->visible(function (Comment $record) {
return $record->approved_at === null;
})
->visible(function (Comment $record) {
return $record->approved_at === null;
})
and I used this to update the pivot attribute
->action(function (Comment $record) {
return $record->pivot->approved_at = now()
})
->action(function (Comment $record) {
return $record->pivot->approved_at = now()
})
I hope this helps someone πŸ™ƒ
Want results from more Discord servers?
Add your server