2 Unwanted code
source | link

Following way you get Subcategories by category Id.

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;
}

$categoryId = 4;
$category = $this->categoryFactory->create()->load($categoryId);

$subcategories = $category->getChildrenCategories();
$subCategoryOptions = array();
foreach ($subcategories as $subcategorie) {
    echo $subcategorie->getId();
    echo $subcategorie->getName();
}

Following way you get Subcategories by category Id.

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;
}

$categoryId = 4;
$category = $this->categoryFactory->create()->load($categoryId);

$subcategories = $category->getChildrenCategories();
$subCategoryOptions = array();
foreach ($subcategories as $subcategorie) {
    echo $subcategorie->getId();
    echo $subcategorie->getName();
}

Following way you get Subcategories by category Id.

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;
}

$categoryId = 4;
$category = $this->categoryFactory->create()->load($categoryId);

$subcategories = $category->getChildrenCategories();
foreach ($subcategories as $subcategorie) {
    echo $subcategorie->getId();
    echo $subcategorie->getName();
}
1
source | link

Following way you get Subcategories by category Id.

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;
}

$categoryId = 4;
$category = $this->categoryFactory->create()->load($categoryId);

$subcategories = $category->getChildrenCategories();
$subCategoryOptions = array();
foreach ($subcategories as $subcategorie) {
    echo $subcategorie->getId();
    echo $subcategorie->getName();
}