【发布时间】:2021-05-14 12:04:16
【问题描述】:
我想在 addselect() 函数中求和,但它显示错误。 我有 2 个模型,如下所示:
1.jewelItem 型号:
protected $table = 'jewel_items';
public function buyInvoice(){
return $this->belongsTo(BuyInvoice::class,'buy_invoice_id');
}
2.buyInvoice模型:
protected $table = 'buy_invoices';
public function jewelsItems(){
return $this->hasMany(JewelsItems::class);
}
每个jewelItem 都有重量列。 我的查询:
$buyInvoice=BuyInvoice::addSelect(['allWeight'=>JewelsItem::whereColumn('buy_invoices.id','buy_invoice_id')->sum('weight')
])->get();
但它显示了这个错误:
Column not found: 1054 Unknown column 'buy_invoices.id' in 'where clause' (SQL: select sum(`weight`) as aggregate from `jewel_items` where `buy_invoices`.`id` = `buy_invoice_id`)
如何在不使用 Raw 方法的情况下解决此问题,因为 here 说“原始语句将作为字符串注入到查询中”并且很容易受到攻击。
【问题讨论】:
标签: laravel eloquent query-builder