我需要显示迭代数组的先前值,这是代码
<tr>
<th></th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Delivery Address</th>
<th>Contact No.</th>
<th>Zip Code</th>
<th>Payment Terms</th>
<th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
@foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue=$value->customer_name }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
@endforeach
我需要在迭代中显示客户的名字吗? 关于如何执行此操作的任何想法?
答案 0 :(得分:2)
最后设置值,我首先设置空白
@foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue or ""}}</td>
.....
.....
</tr>
<?php $previousValue = $value->customer_name ?>
@endforeach
答案 1 :(得分:0)
$ customer_count [$ key-1]
当然,您需要检查是否存在或使用null-coalesce运算符之类的东西,所以:
@php
$previous = $customer_count[$key - 1] ?? false;
@endphp
然后使用:
@if( $previous )
... some stuff ...
@endif
您需要注射的任何地方。
答案 2 :(得分:0)
请尝试这个
<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>
您可以了解有关foreach循环here的更多信息。
答案 3 :(得分:0)
首先将先前的值设置为空白,然后在$previousValue
的底部设置值
<?php $previousValue = '';?>
@foreach($customer_count as $key => $value)
<tr>
<td>{{ $previousValue }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
<?php $previousValue = $value->customer_name; ?>
@endforeach