【发布时间】:2020-05-19 11:43:32
【问题描述】:
在我的 Laravel 6.x 项目中,我有 Product 模型、Warehouse 和 WarehouseProduct 模型。
在产品中,我存储了我的产品的基本信息。在 WarehouseProduct 中,我存储有关仓库中产品的库存数量信息。当然,我有很多仓库,里面有很多产品。
我的Product 看起来像这样:
class Product extends Model
{
protected $fillable = [
'name',
'item_number',
// ...
];
}
Warehouse 看起来像这样:
class Warehouse extends Model
{
protected $fillable = [
'name',
'address',
// ...
];
public function products() {
return $this->hasMany(WarehouseProduct::class);
}
public function missingProduct() {
// here I need to return a Product collection which are not in this Warehouse or the
// stored amount is 0
}
}
最后WarehouseProduct 看起来像这样:
class WarehouseProduct extends Model
{
protected $fillable = [
'product_id',
'warehouse_id',
'amount',
// ...
];
public function product() {
return $this->belongsTo(Product::class, 'product_id');
}
public function warehouse() {
return $this->belongsTo(Warehouse::class, 'warehouse_id');
}
如何获得未存储在Warehouse 或金额为0 的Product 集合?
【问题讨论】:
-
您是指一个特定的 Warehouse 实例还是任何一个?
标签: php laravel eloquent eloquent-relationship