smoothtire
smoothtire
FFilament
Created by smoothtire on 4/8/2024 in #❓┊help
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();
2 replies
FFilament
Created by smoothtire on 4/2/2024 in #❓┊help
Count SaleItems sold by Product ID
Hello, I have a Sales Resource with (hasMany) SaleItems Resource which selects a product to sell from the Product table. I'm looking to have a column in my Product table that sums all the related product sales to give a Sold total. I've read through the docs and finding this particular issue to be a little tricky. Any help is appreciated!
13 replies