$catcount = 2; $parents_ids = []; for ($i = 0; $i < $catcount; $i++) { $parents_ids[] = $userCategory[$i]; } $subCats = Category::whereIn('parent_id', $parents_ids)->pluck('id');