17

I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:

$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';

And so on. In my blade template I have the following code:

            {{ $i=0 }}
            @foreach($cats as $cat)

                    {{ $cat['name'] }}<br />

                {{ $i++ }}

            @endforeach

Which outputs:

0 First Category
1 Second Category
2 Third Category

Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?

Thanks.

6 Answers 6

71

You just need to use the plain php translation:

@foreach ($collection as $index => $element)
   {{$index}} - {{$element['name']}}
@endforeach

EDIT:

Note the $index will start from 0, So it should be {{ $index+1 }}

Sign up to request clarification or add additional context in comments.

Comments

11

The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:

<?php $i=0 ?>

@foreach($cats as $cat)
    {{ $cat['name'] }}<br />
<?php $i++ ?>
@endforeach

As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.

3 Comments

Oh right! OK, I see now. Thanks. Thanks also for the advice- do you have any links to where I can read about "eloquent collection objects"?
No problem - You're gonna love Laravel. Here is the obvious place for docs on eloquent collections laravel.com/docs/eloquent#collections but this one is also a very good resource daylerees.com/codebright/eloquent-collections
Awesome. Yes, I've actually been working my way through the Dayle Rees tutorial. I realise now that my initial question was incorrect, $cats is indeed already a collection object rather than a simple array, I just didn't realise it! Thanks.
1
@foreach($cats as $cat)
    {{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
@endforeach

1 Comment

Your $i will always reset to 0
0
<? php $i = 0 ?>
@foreach ( $variable_name as $value )
    {{ $ value }}<br />
< ? php $i++ ?>
@endforeach

Comments

0

if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after

@foreach ($posts as $k => $post)
  {{ $loop->iteration }}. {{ $post->name }}
@endforeach

Comments

-1

You can actually use a built in helper for this: {{ $cat->incrementing }}.

1 Comment

As far as I know $cat->incrementing would indicate if the IDs are auto-incrementing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.