I have a Laravel backend and a VueJS frontend.
I'm trying to produce a query on a model with a hasOne relationship but only select specific columns from the relationship, do a sum and a group by.
Models
- Contract (CONTRACT_TYPE, CONTRACT_PRICE)
- AdditionalInfo (START_DATE)
My Eloquent Query
public function contractsByYear() {
return Contract::where('CONTRACT_TYPE','LIKE','SP-%')->with(['AdditionalInfo' => function($query) {
$query->selectRaw('MONTH(START_DATE) as Month')->whereYear('START_DATE','2019');
}])->sum('CONTRACT_PRICE')->groupBy('Month');
}
Expected Results
MONTH | TOTAL
1 | 500
2 | 233
3 | 800
etc
My issue is the table structure is existing and I can't edit. Why the START_DATe is stored in a separate table is beyond me.