'How to foreach categories from 3rd index from my database / Laravel?

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:)



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nice_dev