Dynamic number of rows in Laravel Blade
Asked Answered
I

9

15

I want a dynamic number of rows in a table like this.

number   name
1        Devy

This my Blade template.

<thead>
        <th>number</th>
        <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $value)
        <tr>
            <td></td>
            <td>{{$value->name}}</td>
        </tr>
    @endforeach
</tbody>

How do I do that?

Interrupted answered 9/5, 2015 at 17:15 Comment(1)
You can use name="myname[]"Chaddie
L
12

This is correct:

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

And you must use index+1 because index starts from 0.

Using raw PHP in view is not the best solution. Example:

<tbody>
    <?php $i=1; @foreach ($aaa as $value)?>

    <tr>
        <td><?php echo $i;?></td>
        <td><?php {{$value->name}};?></td>
    </tr>
   <?php $i++;?>
<?php @endforeach ?>

in your case:

<thead>
    <th>number</th>
    <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $index => $value)
        <tr>
            <td>{{$index}}</td> // index +1 to begin from 1
            <td>{{$value}}</td>
        </tr>
    @endforeach
</tbody>
Lamplighter answered 9/5, 2015 at 17:22 Comment(4)
unless $collection (or $aaa) has a custom index numbering like record id that might not be incremental or filtered.Voile
For laravel 5.3+ use this, better solution.Interrupted
This is not working if you sortby() your collection. The $index will display in the wrong order because it is refering to the collection items sequence before it is sorted. The {{$loop->iteration}} works better.Underslung
correct one.. +1Gilliette
B
23

Try $loop->iteration variable.

`

<thead>
     <th>number</th>
     <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $value)
        <tr>
            <td>{{$loop->iteration}}</td>
            <td>{{$value}}</td>
        </tr>
    @endforeach
</tbody>

`

Bengal answered 2/2, 2019 at 17:23 Comment(3)
This must be the standard answer without any hack of +1.Implacental
This answer is straightforward and correct.Lunular
For those who might be wondering about the variable $loop, it is a named tag so don't worry about it being undefined; v5.3 and up.Remove
L
12

This is correct:

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

And you must use index+1 because index starts from 0.

Using raw PHP in view is not the best solution. Example:

<tbody>
    <?php $i=1; @foreach ($aaa as $value)?>

    <tr>
        <td><?php echo $i;?></td>
        <td><?php {{$value->name}};?></td>
    </tr>
   <?php $i++;?>
<?php @endforeach ?>

in your case:

<thead>
    <th>number</th>
    <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $index => $value)
        <tr>
            <td>{{$index}}</td> // index +1 to begin from 1
            <td>{{$value}}</td>
        </tr>
    @endforeach
</tbody>
Lamplighter answered 9/5, 2015 at 17:22 Comment(4)
unless $collection (or $aaa) has a custom index numbering like record id that might not be incremental or filtered.Voile
For laravel 5.3+ use this, better solution.Interrupted
This is not working if you sortby() your collection. The $index will display in the wrong order because it is refering to the collection items sequence before it is sorted. The {{$loop->iteration}} works better.Underslung
correct one.. +1Gilliette
V
7

Use a counter and increment its value in loop:

<thead>
        <th>number</th>
        <th>name</th>
</thead>
<tbody>
    <?php $i = 0 ?>
    @foreach ($aaa as $value)
    <?php $i++ ?>
        <tr>
            <td>{{ $i}}</td>
            <td>{{$value->name}}</td>
        </tr>
    @endforeach
</tbody>
Voile answered 9/5, 2015 at 17:20 Comment(0)
I
5

Use $loop variable

refer this link Loop Variable

Isthmian answered 8/8, 2017 at 7:58 Comment(0)
H
2

Starting from Laravel 5.3, this has been become a lot easier. Just use the $loop object from within a given loop. You can access $loop->index or $loop->iteration. Check this answer: https://laracasts.com/discuss/channels/laravel/count-in-a-blade-foreach-loop-is-there-a-better-way/replies/305861

Hellenist answered 5/8, 2018 at 10:36 Comment(0)
F
1

Just take a variable before foreach() like $i=1. And increment $i just before foreach() ends. Thus you can echo $i in the desired <td></td>

Faxun answered 9/5, 2015 at 17:20 Comment(0)
M
1

try the following:

<thead>
    <th>number</th>
    <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $index => $value)
        <tr>
            <td>{{$index}}</td>
            <td>{{$value}}</td>
        </tr>
    @endforeach
</tbody>
Monostrophe answered 9/5, 2015 at 17:25 Comment(1)
This answer is not correct. Because the question means something else. If there is a 'where' condition in the query then this numbering will be wrong.Stria
G
1

You can Use it like this in Blade. I hope this will help.

<thead>
    <th>number</th>
    <th>name</th>
</thead>
<tbody>
    @foreach ($aaa as $index => $value)
        <tr>
            <td>{{$index +1}}</td>
            <td>{{$value}}</td>
        </tr>
    @endforeach
</tbody>

Note: {{$index +1}} since $index starts from 0

Gratia answered 3/7, 2020 at 8:59 Comment(0)
H
1

Laravel 8 -

<?php $serial = "1"; ?> - Before @foreach loop
<td>{{ $serial++ }}</td> - Inside @foreach loop
Hamrick answered 2/1, 2022 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.