i want to print 12 articles with pagers using bootstrap 3 theming:
@foreach($category->articles()->orderBy('created_at', 'DESC')->paginate(12) as $article)
<div class="col-md-4">
<a href="/article/{{ $article->slug }}"><img class="img-responsive" src="/imagecache/mainart/{{ $article->image }}" /></a>
<p>{{ strip_tags(str_limit($article->body, $limit = 90, $end = '...')) }}</p>
</div><!--/col-md-4-->
@endforeach
However, i need to print div with class "row" with 3 columns inside.
array_chunk()
won't work in my case, because i am printing category related articles (one to many relationship) and it's object, not array.
Three Equal ColumnsUse the . col class on a specified number of elements and Bootstrap will recognize how many elements there are (and create equal-width columns). In the example below, we use three col elements, which gets a width of 33.33% each.
You can easily nest grids using bootstrap by adding additional rows. Here is a step-by-step guide for this process. Within the desired column, create another element with class row . This second row element will contain your nested grid.
col-md- stands for column medium ≥ 992px. col-xs- stands for column extra small ≥ 768px. The pixel numbers are the breakpoints, so for example col-xs is targeting the element when the window is smaller than 768px(likely mobile devices)...
There are 12 template columns available per row, allowing you to create different combinations of elements that span any number of columns. Column classes indicate the number of template columns to span (e.g., col-4 spans four). width s are set in percentages so you always have the same relative sizing.
You have two options.
array_chunk()
by converting your results to an array firstbreak_array()
which allows you to do the same thing as array_chunk()
but on objectsOption 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:
Place 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 view:
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With