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