我有 2 个表shipments
,发货shipment_batches
包含shipment_batch_id
shipment_batches
外键。在shipment_batches
有一个列shipment_date
.
在我的出货模型中,我有这种关系
public function shipment_batch()
{
return $this->belongsTo(ShipmentBatch::class, 'shipment_batch_id');
}
现在我想根据shipment_date
shipment_batches
过滤和显示所有货件。
$from = $this->_filters['shipment_batch_shipment_date']["'from'"];
$to = $this->_filters['shipment_batch_shipment_date']["'to'"];
$model = $model->shipment_batch();
$model = $model->whereBetween('shipment_date', [$from, $to]);
但得到了不好的方法例外说
Call to undefined method IlluminateDatabaseQueryBuilder::shipment_batch()
我错在哪里? 什么是正确的做法?
我试图以此作为参考
拉拉维尔在关系中不起作用
您可以使用 whereMust 来查询关系,或者使用 where 来预先加载关系。 您似乎只需要具有特定批次日期的货件,因此您无需加载关系,只需查询即可。 一个例子是:
Shipment::whereHas('shipment_batch', function($q) {
$from = $this->_filters['shipment_batch_shipment_date']["'from'"];
$to = $this->_filters['shipment_batch_shipment_date']["'to'"];
$q->whereBetween('shipment_date', [$from, $to]);
});