Bagisto Forum

    Bagisto

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    How to get all categories for a product?

    General Discussion
    2
    4
    1016
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      Volodymyr last edited by

      I tried to use for example

      V 1 Reply Last reply Reply Quote 0
      • V
        Volodymyr @Volodymyr last edited by

        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]

        1 Reply Last reply Reply Quote 0
        • R
          rahul last edited by

          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

          V 1 Reply Last reply Reply Quote 0
          • V
            Volodymyr @rahul last edited by

            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 ) {

            1 Reply Last reply Reply Quote 0
            • First post
              Last post