join('nodes', 'locations.id', '=', 'nodes.location') ->groupBy('locations.id') ->get(); foreach($locations as &$location) { $location->nodes = explode(',', $location->nodes); } return $locations; } }