Last Updated: February 25, 2016
·
2.049K
· codegefluester

Getting the iteration index in Laravel's render_each function

With Laravel's render_each function you can display each item in an array using a partial. This is very handy if you display blog posts or a list of items in a shopping cart.

If you need to access the index of the current iteration, you can easily access it in your partial through the $key variable.

Example

application/views/myview.blade.php

<div class="posts">
    {{render_each('partials.post', $posts, 'post')}}
</div>

application/views/partials/post.blade.php

<div class="post">
     Iteration index: {{$key}}
</div>

If you heard of Laravel for the first time, you should definitely check it out at http://laravel.com. It is a nice PHP framework for web applications and makes developing such apps a breeze.