Display value of Pivot Table

Hello, I'm currently looking for a better and more performant solution to display a pivot table attribute in my relationManager. It is working right now and using formatStateUsing() but it is doing a lot of DB queries as you might see i use the SponsorType::find() function. SponsorsRelationManager.php
public static function table(Table $table): Table
{
return $table
->columns([
ImageColumn::make('logo_url')
->label('Logo')
->circular()
,
TextColumn::make('name')
->searchable()
->sortable(),
TextColumn::make('country')
->searchable()
->sortable(),
TextColumn::make('sponsor_type_id')
->label('Type')
// Show the name of the sponsor type instead of the ID
->formatStateUsing(function (string $state) {
return SponsorType::find($state)->name;
})
->searchable()
->sortable(),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
ImageColumn::make('logo_url')
->label('Logo')
->circular()
,
TextColumn::make('name')
->searchable()
->sortable(),
TextColumn::make('country')
->searchable()
->sortable(),
TextColumn::make('sponsor_type_id')
->label('Type')
// Show the name of the sponsor type instead of the ID
->formatStateUsing(function (string $state) {
return SponsorType::find($state)->name;
})
->searchable()
->sortable(),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
10 Replies
BuggerSee
BuggerSeeOP2y ago
Pivot Class: ConferenceSponsor.php
class ConferenceSponsor extends Pivot
{
protected $table = 'conference_sponsor';

protected $fillable = [
'conference_id',
'sponsor_id',
'sponsor_type_id',
];

public function conference()
{
return $this->belongsTo(Conference::class);
}

public function sponsor()
{
return $this->belongsTo(Sponsor::class);
}

public function sponsorType()
{
return $this->belongsTo(SponsorType::class);
}
}
class ConferenceSponsor extends Pivot
{
protected $table = 'conference_sponsor';

protected $fillable = [
'conference_id',
'sponsor_id',
'sponsor_type_id',
];

public function conference()
{
return $this->belongsTo(Conference::class);
}

public function sponsor()
{
return $this->belongsTo(Sponsor::class);
}

public function sponsorType()
{
return $this->belongsTo(SponsorType::class);
}
}
Sponsor model class:
class Sponsor extends Model
{
protected $table = 'sponsors';

protected $fillable = [
'name',
'country',
'logo_url',
];

public function conferences()
{
return $this->belongsToMany(Conference::class, 'conference_sponsor')
->using(ConferenceSponsor::class)
->withPivot('sponsor_type_id');
}
}
class Sponsor extends Model
{
protected $table = 'sponsors';

protected $fillable = [
'name',
'country',
'logo_url',
];

public function conferences()
{
return $this->belongsToMany(Conference::class, 'conference_sponsor')
->using(ConferenceSponsor::class)
->withPivot('sponsor_type_id');
}
}
BuggerSee
BuggerSeeOP2y ago
awcodes
awcodes2y ago
can you not just eager load the relationship in the eloquent query for the table?
BuggerSee
BuggerSeeOP2y ago
Where exactly would i need to do that? Sorry im quite new to this
awcodes
awcodes2y ago
In your relation manage class I think it’s getTableQuery.
awcodes
awcodes2y ago
Filament
Listing records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
BuggerSee
BuggerSeeOP2y ago
protected function getTableQuery(): Builder
{
return parent::getTableQuery()->with('sponsorType');
}
protected function getTableQuery(): Builder
{
return parent::getTableQuery()->with('sponsorType');
}
BuggerSee
BuggerSeeOP2y ago
Added this code now and i don't get n+1 warning anymore. This should mean the eager loading is working correct?
awcodes
awcodes2y ago
correct then you should be able to just use 'sponsorType.name' for your text column
BuggerSee
BuggerSeeOP2y ago
Thanks it worked 🙂
Want results from more Discord servers?
Add your server