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();
0 Replies