F
Filament2y ago
Abi

Binding TextColumn with a BelongsToMany relation

How can I bind a TextColumn with a belongsToMany relation. Not sure if I am making a mistake. For a customer_orders, I have an addresses with a BelongsToMany relationship because a customer can have a billing and a shipping address. The customers name is stored on the address table and I want to show that on the Order resource on filament. How do I go about doing this? should I use a ViewColumn? I tried this, will it work
TextColumn::make('customer_name')->formatStateUsing(function ($state, $record) {
$shippingAddress = $record->shippingAddress->first();
return $shippingAddress->first_name . ' ' . $shippingAddress->last_name;
}),
TextColumn::make('customer_name')->formatStateUsing(function ($state, $record) {
$shippingAddress = $record->shippingAddress->first();
return $shippingAddress->first_name . ' ' . $shippingAddress->last_name;
}),
4 Replies
Abi
AbiOP2y ago
ok, got this to work with getStateUsing instead of formatStateUsing. Here is the docs that helped with it https://filamentphp.com/docs/2.x/tables/columns/getting-started#calculated-state
Filament
Getting started - Columns - Table Builder - Filament
The elegant TALL stack table builder for Laravel artisans.
Abi
AbiOP2y ago
The only problem with this approach is, I am not able to use searchable with this Any idea how I can make it searchable?
Dan Harrin
Dan Harrin2y ago
maybe searchable(['shippingAddress.first_name', 'shippingAddress.last_name'])
Abi
AbiOP2y ago
thank you. let me try this @danharrin I get this error Column not found: 1054 Unknown column 'shippingAddress.first_name' in 'where clause' Its looking for it on the parent table and not the pivot
Want results from more Discord servers?
Add your server