I am trying to combine 4 models to get a summary report. These are my tables.
user
-id
-name
-country_id
country
-id
-name
invoice
-id
-user_id
-type_id
type
-id
-name
I want to count a detail of invoice for each type based on the country. That is something like this.
country | type_name_1 | type_name_2 | type_name_2
America | 10 | 2 | 4
Canada | 62 | 0 | 35
China | 23 | 9 | 5
I tried the following query but it's not exactly giving the answer I want above.
\App\Invoice::all()->groupBy(function($s){
return $s->user->country->name;
})->groupBy(function($s){
return $s->type->count();
})
Error: Exception with message 'Property [type] does not exist on this collection instance.'
Can someone give me a pointer here?