Laravel select 中的子查询使用查询生成器
Laravel Sub queries in select using query builder
我想使用查询生成器获取此查询:
SELECT *,
( SELECT sum(vendor_quantity)
from inventory
WHERE product_id = products.id
) as qty from products
我被这部分卡住了
(SELECT sum(vendor_quantity) from inventory where product_id = products.id)
我可以使用原始查询来做到这一点,但我想知道是否有一种方法可以在查询构建器中做到这一点。
我的 Table 产品架构:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_type',50);
$table->string('product_name',255);
$table->string('internal_reference',255);
$table->string('barcode',255);
$table->decimal('sale_price', 10, 2);
$table->decimal('cost', 10, 2);
$table->decimal('weight', 10, 2);
$table->decimal('volume', 10, 2);
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('products', function(Blueprint $table) {
$table->foreign('added_by')->references('id')->on('users');
});
股票Table:
Schema::create('stocks', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('vendor')->unsigned();
$table->string('vendor_product_code',255);
$table->string('vendor_product_name',255);
$table->integer('vendor_quantity');
$table->decimal('vendor_price', 10, 2);
$table->date('vendor_produce');
$table->date('vendor_expiry');
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('stocks', function(Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('vendor')->references('id')->on('customers');
$table->foreign('added_by')->references('id')->on('users');
});
您能添加您确切需要的输出吗?比如您打算在您的视图中添加什么,以便我可以为您提供 eloquent 设置。从上面的迁移来看,您似乎缺少一些 table,例如 "inventory"。
无论如何 - 您首先需要设置模型之间的 eloquent 关系。对于以上两个,是这样的:
class Stock extends Model{
public function product(){
return $this->belongsTo(Product::class);
}
}
和
class Product extends Model{
public function stock(){
return $this->hasMany(Stock::class);
}
}
现在,你的总和让我有点困惑......因为vendor_quantity是你股票中的一个栏目table......你需要得到所有的产品和相应的来自股票 table 的外键值,然后将 vendor_quantity 中的所有值相加?如果是这种情况,请执行以下操作:
$products = Products::with('stock')->get();
这将 return eloquent 集合,其中包含您的所有产品和来自库存 table 的外键值。由于您拥有相关 table 中的值,因此您可以遍历每个值并将其添加到变量或将其附加到初始对象以传递给您的视图。例如
$products = Product::with('stock')->get();
foreach ($products as $key => $product){
$vendorQuantitySum = $product->stock->sum('vendor_quantity');
$products[$key]->vendorQuantity = $vendorQuantitySum;
}
现在,当您将 $products 传递给您的视图时,您可以像这样在视图中轻松获得总和:
{{ $product->vendorQuantity }}
我刚刚在我的 Laravel 安装上对其进行了测试,它似乎可以工作:)
我想使用查询生成器获取此查询:
SELECT *,
( SELECT sum(vendor_quantity)
from inventory
WHERE product_id = products.id
) as qty from products
我被这部分卡住了
(SELECT sum(vendor_quantity) from inventory where product_id = products.id)
我可以使用原始查询来做到这一点,但我想知道是否有一种方法可以在查询构建器中做到这一点。
我的 Table 产品架构:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_type',50);
$table->string('product_name',255);
$table->string('internal_reference',255);
$table->string('barcode',255);
$table->decimal('sale_price', 10, 2);
$table->decimal('cost', 10, 2);
$table->decimal('weight', 10, 2);
$table->decimal('volume', 10, 2);
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('products', function(Blueprint $table) {
$table->foreign('added_by')->references('id')->on('users');
});
股票Table:
Schema::create('stocks', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->integer('vendor')->unsigned();
$table->string('vendor_product_code',255);
$table->string('vendor_product_name',255);
$table->integer('vendor_quantity');
$table->decimal('vendor_price', 10, 2);
$table->date('vendor_produce');
$table->date('vendor_expiry');
$table->integer('added_by')->unsigned();
$table->timestamps();
});
// Foreign Keys
Schema::table('stocks', function(Blueprint $table) {
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('vendor')->references('id')->on('customers');
$table->foreign('added_by')->references('id')->on('users');
});
您能添加您确切需要的输出吗?比如您打算在您的视图中添加什么,以便我可以为您提供 eloquent 设置。从上面的迁移来看,您似乎缺少一些 table,例如 "inventory"。
无论如何 - 您首先需要设置模型之间的 eloquent 关系。对于以上两个,是这样的:
class Stock extends Model{
public function product(){
return $this->belongsTo(Product::class);
}
}
和
class Product extends Model{
public function stock(){
return $this->hasMany(Stock::class);
}
}
现在,你的总和让我有点困惑......因为vendor_quantity是你股票中的一个栏目table......你需要得到所有的产品和相应的来自股票 table 的外键值,然后将 vendor_quantity 中的所有值相加?如果是这种情况,请执行以下操作:
$products = Products::with('stock')->get();
这将 return eloquent 集合,其中包含您的所有产品和来自库存 table 的外键值。由于您拥有相关 table 中的值,因此您可以遍历每个值并将其添加到变量或将其附加到初始对象以传递给您的视图。例如
$products = Product::with('stock')->get();
foreach ($products as $key => $product){
$vendorQuantitySum = $product->stock->sum('vendor_quantity');
$products[$key]->vendorQuantity = $vendorQuantitySum;
}
现在,当您将 $products 传递给您的视图时,您可以像这样在视图中轻松获得总和:
{{ $product->vendorQuantity }}
我刚刚在我的 Laravel 安装上对其进行了测试,它似乎可以工作:)