Q:

how to separate lists into different divs dynamically

Good day,
I have a list of categories in my page, but anytime it could be added or removed. Can you please help me formulate a loop to divide my lists into a maximum of 5 list per div.
ex.
foreach($categories as $cat)
  <label><input type ="checkbox"><?php echo $cat->name ?></label></br>
endforeach

the above code will generate a list of checkboxes in 1 column

so I tried this loop
<?php $count = 0; ?>
    @foreach($categories as $cat)
    @if($count == 0)
    <div class="col-md-4">
    @elseif($count == 5)
    <?php $count = 0; ?>
    </div>
    @endif
    <label><input type="checkbox" value="{{$cat->xid}}">{{$cat->category}}</label><br>
    <?php $count++; ?>
    @endforeach

and it generates like this

but it only wraps the first 5 elements on the list and the next 5 is not working. Can you help me please :) suggestions are really appreciated

A:

The below is your code to which I modified would generate the required layout. you will need to apply CSS to the class="col-md-4" to seperate each block as per your requirement.
<?php $count = 0; ?>
 @foreach($categories as $cat)
 @if($count == 0)
   <div class="col-md-4">
 @endif

 @if($count> 0 && $count%5 == 0)
      </div>
      <div class="col-md-4">
 @endif

 <label><input type="checkbox" value="{{$cat->xid}}">{{$cat->category}}     
 </label><br>
 <?php $count++; ?>
@endforeach

@if($count <= 5)
  </div>
@endif

