This question already has an answer here:
I want to get product collection by category id on phtml file. For that I used below code but it's not working.
$collection = $this->productFactory->create() ->getCollection() ->addAttributeToSelect('*') ->addCategoriesFilter(['eq' => 2]);
I also tried this link but when I use addCategoryFilter(\Magento\Catalog\Model\Category $category) in my phtml file it shows me error that unexpected $category on line no. xx
May I need to add anything to get product collection by category id? I am using addCategoriesFilter to get collection by category id. Is it right or I am doing something wrong?