F
Filament12mo ago
Wim

Filament table: related resource with traits

I'm trying to use Laravel-Vouchers in Filament PHP. I have two models: User (implements CanRedeemVouchers) and Product (implement HasVoucher). I cannot 'see' the model Voucher as this is part of the package. So I cannot manually add a relation to between Product and Voucher (however this should be covered by the traits). I created a form where I can add vouchers and assign them to a product. This created an entry in the voucher table:
id:1
code: AAAABBBBCCCC
model_type: App\Models\Product
model_id: 1
expires_at: 2024-1-18
id:1
code: AAAABBBBCCCC
model_type: App\Models\Product
model_id: 1
expires_at: 2024-1-18
In a Filament table 'Vouchers' I would like to display the product name. I was trying as follows:
Tables\Columns\TextColumn::make('name')
->default(function (Product $record) {
return $record->name;
}),
Tables\Columns\TextColumn::make('name')
->default(function (Product $record) {
return $record->name;
}),
But this clearly is not the right way. Anyone can provide little help?
1 Reply
awcodes
awcodes12mo ago
Use dot notation, it will eager load the relationship. Assuming here that 'product' is the name of the relationship.
Tables\Columns\TextColumn::make('product.name')
Tables\Columns\TextColumn::make('product.name')
Want results from more Discord servers?
Add your server