调用未定义的方法 Illuminate\Database\Query\Builder::shipment_batch()

Call to undefined method Illuminate\Database\Query\Builder::shipment_batch()

我有 2 个表 shipmentsshipment_batches 装运时包含 shipment_batch_id shipment_batches 的外键。在 shipment_batches 中有一列 shipment_date.

在我的发货模型中我有这种关系

public function shipment_batch()
{
    return $this->belongsTo(ShipmentBatch::class, 'shipment_batch_id');
}

现在我想用这个过滤并显示 shipment_batches 中基于 shipment_date 的所有货件。

$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 Illuminate\Database\Query\Builder::shipment_batch()

我哪里错了? 做什么才是正确的?

我试着按照这个作为参考

您使用 whereHas to query relations, or with 来预加载关系。看来您只需要具有特定批次日期的 Shipments,因此您不需要加载关系,只需查询它。一个例子是:

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]);
});