首页 > 解决方案 > 其他聚合函数方法在我的 laravel 8 项目中不起作用

问题描述

我有 laravel 框架版本 8.11.2 的 laravel 项目。当我想使用withSum() 等其他聚合函数时,显示错误

我的代码:

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

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

要点:我不想使用原始表达式,因为他们的漏洞

楷模:

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()

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

标签: laraveleloquentlaravel-query-builder

解决方案


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

8.x 发行说明

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

你至少需要 version 8.12.0


推荐阅读