+88 01797-506292

Post Date: March 8, 2019
Posted By: admin

Blade Foreach

With Blade, things become a little simpler. We can use the foreach construct to help us out.

  @if (count($categories ) > 0)    
  <ul>
    @foreach ($categories as $category)
        @include('partials.category', $category)
    @endforeach
 </ul>
  @else
    @include('partials.category-none')
  @endif

we need to define partials and have them call themselves

partials/category.blade.php

    <li>{{ $category['name'] }}</li>
     @if (count($category['children']) > 0)
	<ul>
	   @foreach($category['children'] as $category)
	      @include('partials.category', $category)
	   @endforeach
	</ul>
    @endif      

partials/category-none.blade.php

  <p>You have no category!</p>

Leave a Reply

Your email address will not be published. Required fields are marked *