Reputation: 31
I want to loop the following title of ten into two columns. However, when I try this snippet, it gives me the first and last titles. So what I want is to break the result ten into five of two columns.
@foreach ($allbulletin as $bullets)
@if ($loop->first)
{{$bullets->title}}
@endif
@if ($loop->last)
{{$bullets->title}}
@endif
@endforeach
Upvotes: 3
Views: 535
Reputation: 330
Use this simple array_chunk()
method:
According to documentations:array_chunk, it will automatically make array chunks according to given value
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 5, true));
Output will be like this:
Array ( [0] => Array ( [0] => a [1] => b [2] => c [3] => d [4] => e ) )
Upvotes: 0
Reputation: 489
Another possible solution is to avoid breaking the results at server side to let the browser perform this job.
This way you can simply loop over your results with a @foreach
statement:
@foreach ($allbulletin as $bullets)
<div class="content">
{{$bullets->title}}
</div>
@endforeach
Then you can display it using flex-box (CSS) as in the following snippet.
.container {
display: flex;
flex-wrap: wrap;
width: 200px;
background-color: lightgrey;
}
.content {
width: 80px;
margin: 10px;
background-color: orange;
}
<div class="container">
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
<div class="content">#1</div>
</div>
Upvotes: 0
Reputation: 6233
you can use collection chunk method
<div class="row">
@foreach ($allbulletin->chunk(5) as $chunk)
<div class="col-md-6"
@foreach ($chunk as $bullet)
<p>{{ $bullet->title }}</p>
@endforeach
</div>
@endforeach
</div>
Upvotes: 3