Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap row class for every 3 columns

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.

like image 426
Alexander Kim Avatar asked Aug 17 '14 04:08

Alexander Kim


People also ask

How do I make 3 columns in Bootstrap 4?

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.

Can you put rows in columns Bootstrap?

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.

What does Col MD 5 class means?

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

How many columns do you have in each Bootstrap row?

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.


1 Answers

You have two options.

  1. Use array_chunk() by converting your results to an array first
  2. Use a custom function break_array() 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:

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
like image 157
Laurence Avatar answered Oct 09 '22 01:10

Laurence