【发布时间】: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