lundi 11 mars 2019

laravel: how to check item availability in each category?

I use laravel-nestedset and I can get all categories and its children with this code:

code1:

$categories = $this->traverse(Category::defaultOrder()->get()->toTree(), null);

For checking product availability in each category, I use this code:

code2:

if($category ->products()->exists()) {...}

Can I combine these two codes?

I want The code 1, returns only the categories containing the products.

Currently, I use this code:

code 3:

  $temp = [];
  $categories = $this->traverse(Category::defaultOrder()->get()->toTree(), null);
  foreach($categories as $key=>$value){

      $category = Category::find($key);
      if($category ->products()->exists()){
         $temp[$key] = $value;
      }
  }
  $categories = $temp;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire