Skip to content

Instantly share code, notes, and snippets.

@robertnicjoo
Forked from ericlbarnes/foreach_limit.blade.php
Created February 18, 2018 15:05
Show Gist options
  • Save robertnicjoo/ff4b06dfb03a68c60bb446b1d200c716 to your computer and use it in GitHub Desktop.
Save robertnicjoo/ff4b06dfb03a68c60bb446b1d200c716 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
@robertnicjoo
Copy link
Author

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

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