Laravel Blade中的动态行数 [英] Dynamic number of rows in Laravel Blade

查看:108
本文介绍了Laravel Blade中的动态行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个这样的表中动态的行数.

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

number   name
1        Devy

这是我的Blade模板.

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>

我该怎么做?

推荐答案

这是正确的:

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

并且您必须使用index + 1,因为index从0开始.

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

在视图中使用原始PHP并不是最佳解决方案.示例:

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 ?>

在您的情况下:

<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>

这篇关于Laravel Blade中的动态行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆