Skip to content

Instantly share code, notes, and snippets.

@ericlbarnes
Last active July 13, 2023 07:51
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ericlbarnes/5771365 to your computer and use it in GitHub Desktop.
Save ericlbarnes/5771365 to your computer and use it in GitHub Desktop.
Limit a foreach with Laravel blade
@foreach (array_slice($posts->toArray(), 0, 5) as $post)
<h1>{{ $post['title'] }}</h1>
@endforeach
@bgallagh3r
Copy link

Shouldn't you technically limit the posts via the controller and not via the view? I guess this might be useful when depending on a user set variable to allow them to see a certain number of posts. Just seems that if you're going to request a chunk of data you should do the request before it's sent to the view. Just wondering where you may need to do this in a use case.

@matiit
Copy link

matiit commented Jun 13, 2013

I see usecase for that. When you use the same data two times on the same view. In one place you use all of them, but somewhere else in the same view, you need just 3. Then it's good :)

@ericlbarnes
Copy link
Author

@bgallagh3r yes in an ideal situation you would just have the number you need. But some times you have special circumstances. :)

This particular use case was building a theme for an app and the $posts returned to the view was more than I needed. But other themes needed the additional. If that makes sense.

@briankiewel
Copy link

Even easier...

@foreach ($posts->slice(0, 5) as $post)

  <h1>{{ $post['title'] }}</h1>

@endforeach

@jrmadsen67
Copy link

@briankiewel - where is that doc'd? Like to read more on it

@briankiewel
Copy link

@jrmadsen67 Unfortunately not documented on laravel.com, but you can look at the code here. It's basically just a helper for what @ericbarnes is doing, but without having to run ->toArray()

@ericlbarnes
Copy link
Author

Brilliant! :)

@avxkim
Copy link

avxkim commented Jun 12, 2014

@briankiewel

Nice trick, thanks =)

@mlops
Copy link

mlops commented Apr 16, 2015

Perfect

@mlops
Copy link

mlops commented Apr 16, 2015

In my case I have One album Music with 2 discs. Each Disc 14 musics and musics came from same table music. I share with this just with the model and one single consult.. Thanks a lot its very helpful for me.

@aliharis
Copy link

This is great. Thank you.

@lyovkin
Copy link

lyovkin commented Jul 31, 2015

Perfect :) Thanks

@jvanremoortere
Copy link

Can you do this for an array that gets loaded from the translation files?

For example:

@foreach(trans('pages/team.team') as $teamMember)
   // Do something
@endforeach

But what if I need just the first 4 or 5?

@abetsagaral
Copy link

->take(20)->get();

@ryanbattles
Copy link

Just needed this for a dashboard view where we need the same data in different places, just modified a bit by limiting what is shown. Thanks for the solution, worked perfectly!

@Kras4ooo
Copy link

@foreach ($posts->slice(0, 5) as $post)
  <h1>{{ $post->title }}</h1>
@endforeach

It works perfect for me.

@ron4stoppable
Copy link

 @foreach ($posts->take(5) as $post)
    <h1>{{ $post->title }}</h1>
 @endforeach

is much cleaner, and works great

@powolnymarcel
Copy link

Yes sir ;) @ron4stoppable

@abubakar-iqbal
Copy link

@foreach($skills->user->take(5) as $user)
this one is great

@sajed5208
Copy link

sajed5208 commented Sep 21, 2017

@foreach ($products->take(5) as $product)

{{ $product->product_name}}


@Endforeach

@HninEiEaindray
Copy link

@foreach (array_slice($data, 0, 4) as $content)

@ezewer
Copy link

ezewer commented Mar 27, 2018

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment