В моем проекте 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');
}
Как я могу получить коллекцию Product
, которая не хранится в Warehouse
или сумма равна 0
?