addselect () 함수를 합산하고 싶지만 오류가 표시됩니다. 2 가지 모델이 있습니다.
1. 보석 항목 모델 :
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 메서드를 사용하지 않고 어떻게이 문제를 해결할 수 있습니까? 여기에 "원시 문이 문자열로 쿼리에 삽입 될 것입니다"라고 말하고 있으며 취약합니다.
최신 버전의 laravel에서는 withSum
관련 가중치 합계를 얻는 데 사용할 수 있습니다.jewelsItems
$buyInvoice=BuyInvoice::withSum('jewelsItems', 'weight')
->orderBy('jewelsItems_sum_weight')
->get();
또는 이전 버전에서는 다음 withCount
과 같이 합계를 사용할 수 있습니다.
$buyInvoice= BuyInvoice::withCount([
'jewelsItems as allWeight' => function ($query) {
$query->select(DB::raw("sum(weight)"));
}
])->orderBy('allWeight')
->get();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다