如何在laravel Blade的foreach循环中获得第n行?

问题描述

考虑我有 2 张这样的桌子

剩余 | c_剩余

0 | 10

20 | 30

40 | 50

我想要 10 - 20 、 30 - 40 和 50 - 0 并且我想要它在 foreach 循环中

@foreach($paid_amounts as $pay_amount)
              <tr>
                <td>{{ $pay_amount->c_remaining - $pay_amount->remaining[select 2nd value] }}</td>
              </tr>
              @endforeach

如何跳过第一个值并从当前 1 中获取一个值?

解决方法

您可以尝试在控制器本身中对数据进行整形。

$paid_amounts->map(function($item,$key) use($paid_amounts) {

    $remaining = isset($paid_amounts[$key + 1]) 
        ? $paid_amounts[$key + 1]->remaining 
        : $paid_amounts[0]->remaining;
    
    //Add a new property to each element of the collection
    $item->prop = $item->c_remaining - $remaining;

    return $item;
});

然后在刀片视图中您可以使用新属性

@foreach($paid_amounts as $pay_amount)
    <tr>
        <td>{{ $pay_amount->prop }}</td>
    </tr>
@endforeach
,

您的问题确实不清楚,但这是我目前了解到的......

$arr = [0,10,20,30,40,50];

for ($i=1; $i<sizeof($arr);$i++) {
    echo $arr[$i]." ";
}

echo $arr[0];