You have two options.
- Use
array_chunk() by converting the results to an array first - Use the
break_array() custom function, which allows you to do the same thing as array_chunk() , but on objects
Option 1:
@foreach (array_chunk($category->articles()->orderBy('created_at', 'DESC')->paginate(12)->toArray()['data'], 3, true) as $column) <div class="row"> @foreach ($column as $article) <div class="col-md-4"> <p>{{ strip_tags(str_limit($article['body'], $limit = 90, $end = '...')) }}</p> </div @endforeach </div> @endforeach
Option 2:
Put this in a helper function somewhere in your application:
function break_array($array, $page_size) { $arrays = array(); $i = 0; foreach ($array as $index => $item) { if ($i++ % $page_size == 0) { $arrays[] = array(); $current = & $arrays[count($arrays)-1]; } $current[] = $item; } return $arrays; }
Then, in your opinion:
@foreach (break_array($category->articles()->orderBy('created_at', 'DESC')->paginate(12), 3) as $column) <div class="row"> @foreach ($column as $article) <div class="col-md-4"> <p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p> </div @endforeach </div> @endforeach
Laurence
source share