【问题标题】:Other Aggregate Functions method not work in my laravel 8 project其他聚合函数方法在我的 laravel 8 项目中不起作用
【发布时间】:2021-05-15 10:32:27
【问题描述】:

我有 laravel 框架版本 8.11.2 的 laravel 项目。 当我想使用 Other Aggregate Functions 时,比如 withSum() 显示错误

我的代码:

       $buyInvoice=BuyInvoice::Where([
        ['provider_id',$validatedData['ProviderCode']],
        ['InvoiceDate','>',$fromDate],
        ['InvoiceDate','<',$ToDate],
    ])->withSum('jewelsItems', 'weight')
         ->orderBy('jewelsItems_sum_weight')
         ->get();

描述:我正在尝试汇总我在某些条件下找到的普通发票中所有珠宝项目的重量,并将其存储在每张发票的新列中。

要点:我不想使用Raw Expressions 导致他们的漏洞

型号:

1.jewelItem 型号:

    protected $table = 'jewel_items';

public function jewel(){
    return $this->belongsTo('App\Models\Jewel');
}

public function buyInvoice(){
    return $this->belongsTo(BuyInvoice::class,'buy_invoice_id');
}

2.buyInvoice模型:

    protected $table = 'buy_invoices';

public function provider(){
    return $this->belongsTo(Provider::class,'provider_id');
}

public function jewelsItems(){
return $this->hasMany(JewelsItem::class,'buy_invoice_id');
}

错误:

Call to undefined method App\Models\BuyInvoice::withSum()

它与我的框架有关吗? 我怎样才能彻底解决这个问题?

【问题讨论】:

    标签: laravel eloquent laravel-query-builder


    【解决方案1】:

    你没有足够新的 Laravel 8 版本。

    8.x 版本说明

    v8.12.0 (2020-10-29) ... 将withMax()|withMin()|withSum()|withAvg() 方法添加到Illuminate/Database/Eloquent/Concerns/QueriesRelationships (#34965, f4e4d95, #35004) ...

    您至少需要版本8.12.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2016-05-04
      • 2014-05-31
      相关资源
      最近更新 更多