Home > Net >  How to foreach categories from 3rd index from my database / Laravel?
How to foreach categories from 3rd index from my database / Laravel?

Time:05-15

I am issuing a problem with the following:

I foreach-ed my categories from index 1 to 3 in my blade file.

Code

@foreach($categories->take(3) as $category)
<ul>
<li>{{$category->name}}</li>
</ul>
@endforeach

But now I want to foreach second half of my categories from my database on the other ul list.

How to do that..?

Thanks:)

CodePudding user response:

You can use splice method to popout the first 3 elements and then use another foreach to create another ul list from the other categories like below:

<?php

@foreach($categories->splice(0, 3) as $category)
<ul>
  <li><a>{{ $category->name }}</a>
</ul>
@endforeach

@foreach($categories as $category)
<ul>
  <li><a>{{ $category->name }}</a>
</ul>
@endforeach
  • Related