代码之家  ›  专栏  ›  技术社区  ›  Fil

对未定义方法的调用将点亮\数据库\查询\生成器::装运\u批处理()

  •  -2
  • Fil  · 技术社区  · 6 年前

    我有2张桌子。 shipments shipment_batches 装运时包含 shipment_batch_id 外键 装运批次 . 在 装运批次 有一列 shipment_date .

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

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

    现在,我想筛选和显示基于 发货日期 在里面 装运批次 有了这个。

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

    但有个badmethodException说

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

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

    我试图以此作为参考

    laravel wherebetween in relations not working

    1 回复  |  直到 6 年前
        1
  •  1
  •   Devon Bessemer    6 年前

    你用 whereHas 查询关系,或 with 渴望加载关系。似乎您只需要具有特定批处理日期的装运,因此不需要加载关系,只需查询它。例如:

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