I believe you want an agrregated query with conditional sum of amount for each user within a specific country.
In pure SQL it can done using CASE
statements as
select name,
sum(case when type='income' then amount else 0 end) as income,
sum(case when type='outcome' then amount else 0 end) as outcome
from invoices
where country = :country
group by name
order by name
In query builder it can be tranformed as
$accounts= DB::table("invoices")
->where("country", $country)
->select([ "name",
DB::raw("sum(case when type='income' then amount else 0 end) as income"),
DB::raw("sum(case when type='outcome' then amount else 0 end) as outcome")
])
->groupBy("name")
->orderBy("name")
->get();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…