我有兩個模型、卡片和風險
卡片具有以下列:
1. id
2. weight
3. color //There are four types of colors, green, red, yellow, blue
風險有以下幾點:
1. id
2. name
3. description
4. card_id
我想要實作的是計算有多少風險,按顏色分組,例如:
'color' => 'green'
'count' => '4'
'color' => 'blue'
'count' => '2'
'color' => 'red'
'count' => '6'
到目前為止,我還無法使用 Eloquent 進行此查詢,也許一些 DB::raw 可以提供幫助?
任何幫助,將不勝感激
uj5u.com熱心網友回復:
Risk::select('card.name', DB::raw('count(risks.card_id)'))
->rightJoin('cards', 'card.id', '=', 'risks.card_id')
->groupBy('risks.card_id')
->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432528.html
