我无法在laravel 5.5刀片中循环播放数组
1.-在我的控制器中我查询了一个dynamodb,我正在返回$ arrayRet
$result =$dynamodb->query(array(
'TableName' => 'sigfox',
'KeyConditionExpression' => 'deviceid = :v_hash',
'ExpressionAttributeValues' => array (
':v_hash' => array('S' => '3E6231')
)
));
//echo "Query succeeded.\n";
$arrayRet['signals'] = $result['Items'];
return view('loggers.index', $arrayRet);
2.-在我的刀片中,我有以下代码
@foreach($signals as $signal)
<tr>
<td>{{$signal['payload']['M']['data']['S']}}</td>
</tr>
@endforeach
3.-我得到未定义的索引:数据---所以我已经dd($ signal)这就是结果
array:3 [▼
"payload" => array:1 [▼
"M" => array:10 [▼
"avgSnr" => array:1 [▶]
"rssi" => array:1 [▶]
"data" => array:1 [▼
"S" => "33333b414900"
]
"lng" => array:1 [▶]
"snr" => array:1 [▶]
"station" => array:1 [▶]
"seqNumber" => array:1 [▶]
"time" => array:1 [▶]
"device" => array:1 [▶]
"lat" => array:1 [▶]
]
]
"deviceid" => array:1 [▶]
"timestamp" => array:1 [▶]
]
4.-我也尝试获得硬编码的价值并且它有效。但我必须使用@foreach
的“刀片方式” <tr>
<td>{{$signals[0]['deviceid']['S']}}</td>
<td>{{$signals[0]['timestamp']['S']}}</td>
<td>{{$signals[0]['payload']['M']['avgSnr']['S']}}</td>
<td>{{$signals[0]['payload']['M']['snr']['S']}}</td>
<td>{{$signals[0]['payload']['M']['lat']['S']}}</td>
<td>{{$signals[0]['payload']['M']['lng']['S']}}</td>
</tr>
5.-也许我错过了什么,请告知
答案 0 :(得分:0)
如果你使用PHP 7 null coalescing operator在这里非常有用!
@foreach(signals as signal)
<tr>
<td>{{ $signal['deviceid']['S'] ?? 'N/A' }}</td>
<td>{{ $signal['timestamp']['S'] ?? 'N/A' }}</td>
<td>{{ $signal['payload']['M']['avgSnr']['S'] ?? 'N/A' }}</td>
<td>{{ $signal['payload']['M']['snr']['S'] ?? 'N/A' }}</td>
<td>{{ $signal['payload']['M']['lat']['S'] ?? 'N/A' }}</td>
<td>{{ $signal['payload']['M']['lng']['S'] ?? 'N/A' }}</td>
</tr>
@endforeach
如果索引不存在,它将打印&#34; N / A&#34;作为默认值。
可以有更多改进!在发送视图之前创建一个模型来清理这些索引和所有内容,代码看起来像这样。在控制器中,处理此数据,然后再将其发送到视图。像array_map
这样的函数在这里很有用。逻辑人员应该尽可能地完成视图。你的代码看起来会更整洁。
@foreach(signals as signal)
<tr>
<td>{{ $signal['deviceid'] }}</td>
<td>{{ $signal['timestamp'] }}</td>
<td>{{ $signal['avgSnr'] }}</td>
<td>{{ $signal['snr'] }}</td>
<td>{{ $signal['lat'] }}</td>
<td>{{ $signal['lng'] }}</td>
</tr>
@endforeach