How to get all categories for a product?
-
I tried to use for example
-
I tried to use for example
ProductRepository.php/**
* @param integer $categoryId
* @return Collection
*/
public function getAll($categoryId = null)
{
............
// add my code
foreach ($results as $result) {
$categoriesForProduct = $this->findOrFail($result->id)->categories()->get();
}getting an error:
ErrorException (E_ERROR)
No query results for model [Webkul\Product\Models\Product] -
Hi @Volodymyr
Try this one -
foreach ($results as $result) { $categoriesForProduct = $this->findOrFail($result->id)->categories; }
and Make sure that -
$this->findOrFail($result->id);
returns a product Model.
Thanks
-
Hi @rahul.
Thank you very much. You showed me a way out of the dead end of civilization!
You are right about object verification.
$categoriesForProduct = $productRepository->find($product->id);
if ($categoriesForProduct) {
foreach ($categoriesForProduct->categories()->get() as $categoryProduct ) {