F
Filament3mo ago
pabbec

I have an excess of queries in my Filament Table.

I'm bulding a product manager for my application. I have two tables: products and variants. One product has many variants. I'm using Filament 3.2.118 on Laravel 11
class Product extends Model
{
// ....
public function variants(): HasMany{
return $this->hasMany(ProductVariant::class);
}
}


class ProductVariant extends Model
{
// ....
public function product(): BelongsTo {
return $this->belongsTo(Product::class);
}
}
class Product extends Model
{
// ....
public function variants(): HasMany{
return $this->hasMany(ProductVariant::class);
}
}


class ProductVariant extends Model
{
// ....
public function product(): BelongsTo {
return $this->belongsTo(Product::class);
}
}
And I have a resource Product:
class ProductResource extends Resource
{
protected static ?string $model = Product::class;

// ...
public static function table(Table $table): Table
{
return $table
->columns([
// ..
Tables\Columns\TextColumn::make('price')
->label('Price')
->state( function(Product $record) {
return $record->variants()->first()->priceFormated();
})
])
// Alter query to use eager loading
->modifyQueryUsing(function (Builder $query) {
return $query->with(['brand:name', 'variants:sku,currency,tax,price_value']);
});
}
class ProductResource extends Resource
{
protected static ?string $model = Product::class;

// ...
public static function table(Table $table): Table
{
return $table
->columns([
// ..
Tables\Columns\TextColumn::make('price')
->label('Price')
->state( function(Product $record) {
return $record->variants()->first()->priceFormated();
})
])
// Alter query to use eager loading
->modifyQueryUsing(function (Builder $query) {
return $query->with(['brand:name', 'variants:sku,currency,tax,price_value']);
});
}
` In the model variant, I have a method thats format the price and more. When I do this, Filament makes many queries to database: 37 (see image attachments). It makes the correct queries with eager loading. But then, for each product row it lists in the table, it fires three queries to retrieve the relationship (I don't know why). While I could set up my column like this:_
Tables\Columns\TextColumn::make('variants.price_value')
->label('Price')

Tables\Columns\TextColumn::make('variants.price_value')
->label('Price')

It doesn't work for me, because my Variant model has the methods to format the product price according to taxes and the type of currency associated with the product (currency and tax). What could I be doing wrong or is there any other way to solve this? Best, Pablo
No description
Solution:
I found the problem 🤦‍♂️: I'm using: ```php $record->variants()->first()->price_value...
Jump to solution
2 Replies
toeknee
toeknee3mo ago
You are calling them constantly with your state method.... surely you want:
Tables\Columns\TextColumn::make('variants.price')
->label('Price')
Tables\Columns\TextColumn::make('variants.price')
->label('Price')
or similar, it would depend what your priceFormatted did... but the above should be used to fetch the relationship and then formatState if you want to re-format the price display? Also you can edger load the relationships.
Solution
pabbec
pabbec3mo ago
I found the problem 🤦‍♂️: I'm using:
$record->variants()->first()->price_value
$record->variants()->first()->price_value
instead of
$record->variants->first()->price_value
$record->variants->first()->price_value
In the first case, I'm getting a new instance of Query Builder for each row... In the second case, I'm access to property variants with the related records loaded previously.
Want results from more Discord servers?
Add your server