【发布时间】:2014-07-11 22:12:34
【问题描述】:
我在视图中使用 Laravel 和 Blade 模板。我正在写出一些数据,因为我的列具有相同的名称,最后有一个数字,我想我可以遍历它们而不是全部写出来。
这是我目前拥有的:
我正在传递一个名为 $scores 的对象/数组:
<span>{{ $scores->hole1 }}</span>
<span>{{ $scores->hole2 }}</span>
<span>{{ $scores->hole3 }}</span>
<span>{{ $scores->hole4 }}</span>
<span>{{ $scores->hole5 }}</span>
.. 等等
我想做的是这样的:
@for ($i = 1; $i < 19; $i++)
<span>{{ $scores->hole . ${i} }}</span>
@endfor
但我似乎无法正确地在变量名末尾附加索引值的语法。这是可能的吗,还是我以错误的方式处理这个问题?
$scores 是我在控制器中构建并传递给刀片模板的对象。 $scores 中的数据如下所示:
object(stdClass)[212]
public 'id' => int 3
public 'course_name' => string 'Golf Club A' (length=15)
public 'played_date' => string '2014-02-05' (length=10)
public 's_hole1' => int 5
public 's_hole2' => int 6
public 's_hole3' => int 4
public 's_hole4' => int 5
public 's_hole5' => int 6
public 's_hole6' => int 4
public 's_hole7' => int 4
public 's_hole8' => int 5
public 's_hole9' => int 4
public 's_hole10' => int 3
public 's_hole11' => int 5
public 's_hole12' => int 4
public 's_hole13' => int 5
public 's_hole14' => int 4
public 's_hole15' => int 3
public 's_hole16' => int 4
public 's_hole17' => int 3
public 's_hole18' => int 4
public 't_hole1' => int 4
public 't_hole2' => int 3
public 't_hole3' => int 5
public 't_hole4' => int 4
public 't_hole5' => int 4
public 't_hole6' => int 4
public 't_hole7' => int 4
public 't_hole8' => int 5
public 't_hole9' => int 4
public 't_hole10' => int 3
public 't_hole11' => int 5
public 't_hole12' => int 4
public 't_hole13' => int 5
public 't_hole14' => int 4
public 't_hole15' => int 3
public 't_hole16' => int 4
public 't_hole17' => int 3
public 't_hole18' => int 4
【问题讨论】:
-
这是可能的,但您几乎可以肯定是以错误的方式接近它。为什么
$scores不是数组? -
为什么不把洞做成数组,听起来很奇怪