Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda