© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
1 reply
smoothtire

Avg() column from Relationship

I'm looking to get the average sale price of each product sold and having some formatting issues. Read through this section in the docs and I'm stuck. Any help is appreciated!

TextColumn::make('saleItems_avg_price')->avg([ 
'saleItems' => fn (Builder $query) => $query->where('product_id', equalTo('id')),], 'price')
TextColumn::make('saleItems_avg_price')->avg([ 
'saleItems' => fn (Builder $query) => $query->where('product_id', equalTo('id')),], 'price')

Schema::create('sale_items', function (Blueprint $table) {
$table->id();
$table->foreignId('sale_id')
                ->constrained('sales')
                ->cascadeOnDelete();
$table->foreignId('product_id')
                ->constrained('products')
                ->cascadeOnDelete();
$table->unsignedBigInteger('quantity');
$table->decimal('unit_price');
$table->decimal('price', 10, 2)
                ->nullable();
$table->timestamps();
Schema::create('sale_items', function (Blueprint $table) {
$table->id();
$table->foreignId('sale_id')
                ->constrained('sales')
                ->cascadeOnDelete();
$table->foreignId('product_id')
                ->constrained('products')
                ->cascadeOnDelete();
$table->unsignedBigInteger('quantity');
$table->decimal('unit_price');
$table->decimal('price', 10, 2)
                ->nullable();
$table->timestamps();

 Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('sku')
                ->unique()
                ->nullable();
$table->longText('description')->nullable();
$table->unsignedBigInteger('quantity')->nullable();
$table->decimal('unit_cost', 10, 2)->nullable();
$table->boolean('is_visible')->default(false)->nullable();
$table->enum('type', ['flower', 'edible', 'etc'])->nullable();
$table->unsignedBigInteger('sold')->nullable();
$table->timestamps();
 Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('sku')
                ->unique()
                ->nullable();
$table->longText('description')->nullable();
$table->unsignedBigInteger('quantity')->nullable();
$table->decimal('unit_cost', 10, 2)->nullable();
$table->boolean('is_visible')->default(false)->nullable();
$table->enum('type', ['flower', 'edible', 'etc'])->nullable();
$table->unsignedBigInteger('sold')->nullable();
$table->timestamps();
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Table Column Relationship Sum, Avg, Max, Min, ext being ignored
FilamentFFilament / ❓┊help
2y ago
Column relationship
FilamentFFilament / ❓┊help
3y ago
Relationship Column
FilamentFFilament / ❓┊help
3y ago
Summarize Relationship Column
FilamentFFilament / ❓┊help
3y ago