如何将模型的相关条件添加到Laravel 6.x 中的Eloquent 查询?
How to add model's relationed condition to Eloquent query in Laravel 6.x?
在我的 Laravel 6.x 项目中,我有 Product
模型、ProductCategory
和 WarehouseProduct
模型。
在 Product
我存储了我的产品的基本信息。在 ProductCategory
模型中,我存储了产品的类别信息。在 WarehouseProduct
中,我将有关产品的库存信息存储在仓库中。我当然有很多仓库,有很多产品。
我的产品是这样的:
class Product extends Model
{
protected $fillable = [
'name',
'item_number',
// ...
];
public function categories() {
return $this->belongsToMany(ProductCategory::class, 'product_category_products',
'product_id', 'product_category_id');
}
}
ProductCategory 如下所示:
class ProductCategory extends Model
{
protected $fillable = [
'name',
'description',
// ...
];
public function products() {
return $this->belongsToMany(Product::class, 'product_category_products',
'product_category_id', 'product_id');
}
}
WarehouseProduct 如下所示:
class WarehouseProduct extends Model
{
protected $fillable = [
'product_id',
'warehouse_id',
'amount',
// ...
];
public function product() {
return $this->belongsTo(Product::class, 'product_id');
}
}
我现在有这个查询:
$query = WarehouseProduct::select([
'product_id',
'warehouse_id',
DB::raw('SUM(free_amount)'),
DB::raw('SUM(booked_amount)'),
// ...
]);
if (isset($request->warehouse_id)) {
$query->where([['warehouse_id', '=', $request->warehouse_id]]);
}
if (isset($request->product_category_id)) {
// ???
}
如何向查询添加 where 条件 what said: products from this category?
您可以查询 Relationship Existence. As it is a relationship through another model (Product
) you could reduce the query if you defined that Has Many Through 关系,但我认为这对于这个特定查询来说已经足够了。
$warehouse_id = $request->warehouse_id;
$product_category_id = $request->product_category_id;
$query = WarehouseProduct::select([
'product_id',
'warehouse_id',
DB::raw('SUM(free_amount)'),
DB::raw('SUM(booked_amount)'),
// ...
])
->when($warehouse_id, function ($query) use ($warehouse_id) {
$query->where('warehouse_id', $warehouse_id);
})
->when($product_category_id, function ($query) use ($product_category_id) {
$query->whereHas('product', function ($que) use ($product_category_id) {
$que->whereHas('categories', function ($q) use ($product_category_id) {
$q->where('id', $product_category_id);
})
})
});
$results = $query->get();
请注意,我对 conditional clauses 使用 when
方法,但您可以像之前一样继续 if
s。
在我的 Laravel 6.x 项目中,我有 Product
模型、ProductCategory
和 WarehouseProduct
模型。
在 Product
我存储了我的产品的基本信息。在 ProductCategory
模型中,我存储了产品的类别信息。在 WarehouseProduct
中,我将有关产品的库存信息存储在仓库中。我当然有很多仓库,有很多产品。
我的产品是这样的:
class Product extends Model
{
protected $fillable = [
'name',
'item_number',
// ...
];
public function categories() {
return $this->belongsToMany(ProductCategory::class, 'product_category_products',
'product_id', 'product_category_id');
}
}
ProductCategory 如下所示:
class ProductCategory extends Model
{
protected $fillable = [
'name',
'description',
// ...
];
public function products() {
return $this->belongsToMany(Product::class, 'product_category_products',
'product_category_id', 'product_id');
}
}
WarehouseProduct 如下所示:
class WarehouseProduct extends Model
{
protected $fillable = [
'product_id',
'warehouse_id',
'amount',
// ...
];
public function product() {
return $this->belongsTo(Product::class, 'product_id');
}
}
我现在有这个查询:
$query = WarehouseProduct::select([
'product_id',
'warehouse_id',
DB::raw('SUM(free_amount)'),
DB::raw('SUM(booked_amount)'),
// ...
]);
if (isset($request->warehouse_id)) {
$query->where([['warehouse_id', '=', $request->warehouse_id]]);
}
if (isset($request->product_category_id)) {
// ???
}
如何向查询添加 where 条件 what said: products from this category?
您可以查询 Relationship Existence. As it is a relationship through another model (Product
) you could reduce the query if you defined that Has Many Through 关系,但我认为这对于这个特定查询来说已经足够了。
$warehouse_id = $request->warehouse_id;
$product_category_id = $request->product_category_id;
$query = WarehouseProduct::select([
'product_id',
'warehouse_id',
DB::raw('SUM(free_amount)'),
DB::raw('SUM(booked_amount)'),
// ...
])
->when($warehouse_id, function ($query) use ($warehouse_id) {
$query->where('warehouse_id', $warehouse_id);
})
->when($product_category_id, function ($query) use ($product_category_id) {
$query->whereHas('product', function ($que) use ($product_category_id) {
$que->whereHas('categories', function ($q) use ($product_category_id) {
$q->where('id', $product_category_id);
})
})
});
$results = $query->get();
请注意,我对 conditional clauses 使用 when
方法,但您可以像之前一样继续 if
s。