Pick up Remaining Records

Asked

Viewed 27 times

1

It has a function in Laravel’s ORM that makes the LIMIT of Mysql.

Which is the Take and the Skip.

I’m going to do three Owl Sliders on the page, one under the other. And the images I’m going to put comes from a table in the database. I want all three rides to have the same amount of footage.

So I thought I’d do it like this:

Total de Registros / 3

But this can occur a tithe, for example:

10 / 3 = 3,3333333333333333

But to fix it I make one floor in PHP and it rounds to 3.

Then there would be 3 in one line, 3 in another line and 3 in another line. But there was a missing image that I want to appear in the last line.

The logic would be:

$result = floor($countWorks / 3);

$slider_1 = Works::take($result)->get();
$slider_2 = Works::take($result)->skip($result)->get();
$slider_3 = Works::take($result)->skip($result + $result)->get();

I just want to know this:

1 - There is another way to do this or this is the best way ?

Owl Slider works like this:

<?php 
    $works = R::find('works', 'status = 1');
    $take  = array_chunk($works, 3);

    foreach($take as $work){
?>
    <div class="owl-slider-works">
        <?php 
            foreach($work as $val){
        ?>
            <div class="item">
                <img src="images/works/<?=$val->capa?>" alt="<?=$val->titulo?>">
                <div class="titulo">
                    <h5><?=$val->titulo?></h5>
                </div>
            </div>
        <?php 
            }
        ?>
    </div>
<?php 
    }
?>

You need to stay that way inside foreach. But I don’t understand how chunk will work to do this. How would it be ?

1 answer

1


@Zoom, this is easy if you use the features of Collection of Laravel.

Come on. First you select all the data.

 $slider = Works::all();

Then you can use the method Illuminate\Support\Collection::chunk to "break" the results into specific pieces. In this case, we will break 3 by 3. If you have any "leftovers", PHP will leave it in the last Collection (Chunk creates other Collections)

  @foreach( $slider->chunk(3) as $chunk)
      <div class="owl-slider">
       @foreach($chunk as $slide)
           <div class="item">{{ $slide->image }}</div>
        @endforeach
      </div>
  @endforeach

To better understand the chunk, let’s exemplify with the function array_chunk PHP (which is used internally by Collection):

 $nomes = ['zoom', 'rray', 'wallacemaxters', 'bigown', 'randrade'];

 $de_2_em_2 = array_chunk($nomes, 2);

 print_r($de_2_em_2);

The result of this will be:

[
   ["zoom", "rray",],
   ["wallacemaxters", "bigown",],
   ["randrade",],
]

Note that in the end the value "randrade" was alone, because there were 2 values to combine, then the last group remains containing only the rest.

  • That’s interesting. I didn’t know this method chunk. But let me ask you something... I’ll edit my question.

  • is not working. I put 10 photos... and he printed 4 lines, 3 lines with 3 photos and 1 line with 1... ie 4 lines. I want only 3 lines and the rest... ie 4, 4 and 2... Rsrsrs

  • I upgraded the code to my pole...

  • Must be something wrong with my foreach

  • I solved! I told you yesterday that I had to take the total and split it three ways... and you said, "No... son, Chunk does it alone... nhenhe"... so I gave him a good one.

  • Thanks for the help

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.