vendredi 28 juillet 2017

How to use two foreachs in laravel?

I have a category table, it is organized by 'parent_id' and 'categoryid'. I need to organize it in a list, where I group the parent class with the daughter class.

I created this code.

In the controller I get the value of the categories.

public function index()
    {
      $cat1 = Category::where('erp_parentid', '=', 0)->get();
      foreach($cat1 as $categoria1){
        $cat2 = Category::where('erp_parentid', '=', $categoria1->erp_categoryid)->get();
        return view('admin.categories')->with('cat1', $cat1)->with('cat2', $cat2);
      }
    }

$cat2 is the child category, I get its values through the categoryid of the parent category.

But when I pass the values to the view, all parent categories take the same value as the first.

enter image description here I used that code to display the values in the view:

<div class="container">
  <div class="row">
    <ul class="list-group">
      @foreach($cat1 as $value)
      <a data-toggle="collapse" data-target="#catfilha"><li class="list-group-item"></li></a>
      <ul id="catfilha" class="collapse">
        @foreach($cat2 as $value2)
        <li></li>
        @endforeach
      </ul>
      @endforeach
    </ul>
  </div>
</div>

I searched for similar cases here on the site, but found no resemblance, any suggestions? Thank you in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire