Haven’t managed to figure out a way to actually create a relationship of the type I was hoping for, but I did at least manage a workaround specifically for Filament admin panels, using getStateUsing(). I had tried this earlier, based on the example in the docs:
Tables\Columns\TextColumn::make('amount_including_vat')
->getStateUsing(function (Model $record): float {
return $record->amount * (1 + $record->vat_rate);
})
– but I couldn’t get it to work. I kept getting obscure “Argument #1 ($haystack) must be a string” errors and couldn’t figure out what was causing them. Eventually I stumbled on the closure customisation section in the Filament docs and noticed this phrasing:
If you have defined a form or component Eloquent model instance, define a $record parameter
Turns out the reason it hadn’t worked was that I had named my parameter semantically, as Order $order instead of Model $record. Once I fixed that, it worked:
TextColumn::make('products')
->getStateUsing(function (Model $record) : string {
return $record->items->implode(fn ($i) => $i->product->name(), '<br />');
})
->html()
->label('Products')