Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
Laravel loop collection into small chunks

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
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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>
over 4 years ago · Santiago Trujillo Denunciar

0

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>

over 4 years ago · Santiago Trujillo Denunciar

0

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 ) )
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda