Hello i have this function which sums the budget_cost and get the data by month
public function marktingCost(){
$costs = \DB::table('campaigns')
->select('campaign_leadsource_id', \DB::raw('SUM(budget_cost) as budget_total_month'))
->addselect('campaign_leadsource_id', \DB::raw('SUM(budget_cost) as budget_total_year'))
->groupBy('campaign_leadsource_id')
->where('campaign_status_id',4) // campaign_status_id = 4 means campaign completed
->where(\DB::raw('MONTH(created_at)'), Carbon::today()->month)
->get(); return $costs}
what im trying to achive is get the data by month as budget_total_month and get the data by year as budget_total_year but i can't use if condition inside query i want to do something like this
->select('campaign_leadsource_id', \DB::raw('SUM(budget_cost) as budget_total_month') ->where(\DB::raw('MONTH(created_at)'), Carbon::today()->month))
->addselect('campaign_leadsource_id', \DB::raw('SUM(budget_cost) as budget_total_year') ->where(\DB::raw('Year(created_at)'), Carbon::today()->year))
But of course that's not valid
what i want as output is that
[{"campaign_leadsource_id":1,"budget_total_month":11475,"budget_total_year":134761,"olxTotal":12,"budget_per_lead":11230},{"campaign_leadsource_id":2,"budget_total_month":4221,"budget_total_year":41215,"olxTotal":9,"budget_per_lead":4579}]
thank you in advance