Home > Net >  How can I retrieve 3 values ​with the every id in the foreach method?
How can I retrieve 3 values ​with the every id in the foreach method?

Time:08-16

   @foreach ($options as $ozellik)
                        @if($ozellik->urun_kategori == $urunler->id )

                        <tr>
                            <td> <b> {!!$ozellik->ozellik!!}</b></td>
                            <td>{!!$ozellik->aciklama!!}</td>
                        </tr>

   @endif
   @endforeach

this is my blade

I want to retrieve 3 values for every id. How can i do that. It has to retrieve 3 values for each id.

My Table

For example i want to get 3 of the values for "urun_kategori" = 7

But there are 4 records for "urun_kategori" = 7

This is where is set $options

CodePudding user response:

Without being able to test it since you did not provide a sample dataset. The solution to your problem should look something like this:

$options = $options->groupBy('urun_kategori')
    ->flatMap(function($group) {
      return $group->take(3);
    });

... place that inside your web.php after the line $options = App\Option::all();

Explanation: groupBy as the name suggest groups the collection by the given attribute 'urun_kategori' into subcollections. In the flatMap we limit the the amount of items in each group to 3 - take(3), then flatten the array because we dont need the subcollections anymore.

  • Related