I am giving my first step in LARAVEL and SQL and I am trying to build a query that returns the list of departments I have and, at the same time, count the total of locations in each department and the total of device of each location.
So I tried the following:
$dep = DB::table('departments')
->leftjoin('locations','departments.dep_id','=','locations.department')
->leftjoin('devices','locations.id','=','devices.location')
->select('departments.name',DB::raw('count(locations.id) as locationcount'))
->groupby('departments.name')
->get();
This return me the following:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[name] => Interior Design
[locationcount] => 3
)
[1] => stdClass Object
(
[name] => IT
[locationcount] => 29
)
[2] => stdClass Object
(
[name] => Marketing
[locationcount] => 0
)
[3] => stdClass Object
(
[name] => Operations
[locationcount] => 13
)
)
)
But here the count we are seeing is for the devices and no for the locations. Is there any way to achieve this query? or I need to do a loop after? I am looking for a result like this:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[name] => Interior Design
[locationcount] => 2
[devicecount] => 10
)
....