0

Im having trouble looping an array in my laravel 5.5 blade

1.- In my controller Im querying a dynamodb and I'm returning $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.-In my blade I have the following code

 @foreach($signals as $signal)
        <tr>
            <td>{{$signal['payload']['M']['data']['S']}}</td>  
        </tr>
 @endforeach

3.- I get Undefined index: data --- So I've dd($signal) and this is the result

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.- I also tried getting the value hardcoding the position and It works. But I have to use the "blade way" with @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.- Maybe I'm missing something please advise

N3b0
  • 1
  • 2
  • 1
    You've 3 items in the array, could it be possible that the first item has all the index but not the 2nd or 3rd item? – SteD Feb 20 '18 at 02:45
  • Im not sure how the table was created but yes it is possible. Actually if i do {{$signal['payload']['M']}} in my blade. It tells me that a string is expected but an array is given instead, which make sense. – N3b0 Feb 20 '18 at 02:49
  • As it shows, it's possible that some rows might have inconsistent structure in them. What you have to concern is how will you handle when the target structure is missing. Please tell me your purpose of what you are doing and the output you want, so I can suggest you the appropriate solution on it. – spicydog Feb 20 '18 at 03:01
  • Also in your hardcoding you didn't change the `$signals` index. As a result, it may not move to the next row and then error. – spicydog Feb 20 '18 at 03:03
  • @spicydog I need to present deviceid, timestamp and all the payload elements in a table. If any of the above is missing I just skip it. The information comes from a IoT enabled device which reports every 10 minutes. So it is not a big deal if I miss one. – N3b0 Feb 20 '18 at 03:09
  • @N3b0 are you using PHP7 ? – spicydog Feb 20 '18 at 03:26
  • 1
    Yes. PHP 7.0.22-0ubuntu0.16.04.1 (cli) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.22-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies – N3b0 Feb 20 '18 at 03:33

1 Answers1

0

If you are using PHP 7 null coalescing operator can be very useful here!

@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

If the index doesn't exist, it will print "N/A" as a default value.

There can be more improvement! Create a model to clean up these indexes and everything before sending the view and the code will look something like this. In your controller, process this data before sending it to view. The function like array_map can be useful here. Logic staff should be done out of the views as much as possible. Your code will look much neater.

@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
spicydog
  • 1,644
  • 1
  • 17
  • 32
  • Thank you. I will work on the model. I will try a diferent array not the default DynamoDB Json format. – N3b0 Feb 20 '18 at 03:55