/** * Gets an array of all the allowed screens the specified user is able * to access and modify because they're admin * @param User $user * @param array $allowed_departments * @return EloquentCollection */ public function getAllowedScreens($user, $allowed_departments) { // Return all if a suprer user if ($user->is_super_user) { return Screen::all(); } else { // If the screens assigned department matches // one in the users allowed list then display it $screens = collect([]); foreach ($allowed_departments as $department) { $departmentScreens = $department->Screens()->get(); if ($departmentScreens->count() > 0) { $screens = $screens->merge($departmentScreens); } } } return $screens; }