laravel - 其他聚合函数方法在我的 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()
它与我的框架有关吗?我怎样才能完全解决这个问题?
解决方案
你没有足够新的 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
。
推荐阅读
- python - 为什么我的函数做了它应该做的事情然后它没有继续通过代码?
- ionic3 - 无法更改离子选择弹出模式
- ios - 导入模式下的 UIDocumentPickerViewController 返回 .icloud 文件
- powershell - 以值作为参数的 Powershell 错误
- gmail - 我点击了垃圾邮件中的删除按钮
- vue.js - 如何访问 Vue API 中引用的过滤器?
- python - 如何使用带有 python 和烧瓶的谷歌地方获取附近的地方
- java - Hibernate+SQLServer / 批量只插入新记录
- java - 通过电子邮件发送的范围报告不加载屏幕截图
- go - 卷曲 http-server 可以,但浏览器无法正常工作