F
Filamentβ€’6mo ago
morawcik

Column avg() not working? I'm sure i'm missing something

I don't know if I'm missing something but... I have model Review with relations:
public function product()
{
return $this->belongsTo(Product::class);
}

public function productReviews()
{
return $this->hasManyThrough(Review::class, Product::class, 'id', null, 'product_id');
}
public function product()
{
return $this->belongsTo(Product::class);
}

public function productReviews()
{
return $this->hasManyThrough(Review::class, Product::class, 'id', null, 'product_id');
}
then Product model:
public function reviews()
{
return $this->hasMany(Review::class);
}
public function reviews()
{
return $this->hasMany(Review::class);
}
In Review resource I have this:
return $table
->modifyQueryUsing(function($query) {
$query->with('productReviews');
//$query->withAvg('productReviews', 'rating');
})
->columns([
Tables\Columns\TextColumn::make('product')->state(function (Review $record): string {
return $record->product->translateAttribute('name');
})->label('Produkt'),

Tables\Columns\TextColumn::make('avg')
->avg('productReviews', 'rating')
->label('Avg'),
]);
return $table
->modifyQueryUsing(function($query) {
$query->with('productReviews');
//$query->withAvg('productReviews', 'rating');
})
->columns([
Tables\Columns\TextColumn::make('product')->state(function (Review $record): string {
return $record->product->translateAttribute('name');
})->label('Produkt'),

Tables\Columns\TextColumn::make('avg')
->avg('productReviews', 'rating')
->label('Avg'),
]);
In avg column I have nothing but when I'm using query builder withAvg i'm getting in review ($record) object. What I'm missing with this column avg() ?
Solution:
Oh... thanks πŸ™‚
Jump to solution
2 Replies
Dennis Koch
Dennis Kochβ€’6mo ago
Probably this note from the docs:
In this example, users is the name of the relationship, while age is the field that is being averaged. The name of the column must be users_avg_age, as this is the convention that Laravel uses for storing the result.
https://filamentphp.com/docs/3.x/tables/columns/relationships#aggregating-relationships
Solution
morawcik
morawcikβ€’6mo ago
Oh... thanks πŸ™‚
Want results from more Discord servers?
Add your server