1

I have this function in my block.

public function getSubcategoryByUrlkey($url_key)
{ 
 $categoryCollection = $this->_categoryCollectionFactory->create() 
 ->addAttributeToFilter('url_key',$url_key)->getFirstItem(); 
 echo $categoryCollection->getSelect(); exit;
 return $childs = $categoryCollection->getChildrenCategories();
}

I cannot view anything from this collection. My Magento version is Magento ver. 2.2.2. What is the mistake I am doing here?

echo $categoryCollection->getSelect();

this also not retrieve the query

Update I am getting this error

Fatal error: Uncaught Error: Call to undefined method Magento\Catalog\Model\ResourceModel\Category\Collection::getChildrenCategories()

Utsav Gupta
1,2431 gold badge20 silver badges52 bronze badges
asked Mar 21, 2018 at 12:28

1 Answer 1

2

Remove ->getFirstItem() then it will show the query

$categoryCollection = $this->_categoryCollectionFactory->create() 
 ->addAttributeToFilter('url_key',$url_key); 
echo $categoryCollection->getSelect(); exit;
answered Mar 21, 2018 at 12:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.