• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Register
  • Login
Bagisto Forum

Bagisto

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

How to get all categories for a product?

General Discussion
2
4
1.0k
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 3 Nov 2019, 11:30

    I tried to use for example

    V 1 Reply Last reply 3 Nov 2019, 11:40 Reply Quote 0
    • V
      Volodymyr @Volodymyr last edited by 3 Nov 2019, 11:40

      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 4 Nov 2019, 09:09

        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 4 Nov 2019, 17:41 Reply Quote 0
        • V
          Volodymyr @rahul last edited by 4 Nov 2019, 17:41

          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
          2 out of 4
          • First post
            2/4
            Last post