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(),
            ]);
    }
Was this page helpful?