Loop multidimensional array Laravel - php

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

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

Related

how do i access the array in view after passing it from controller [duplicate]

This question already has answers here:
How to loop multidimensional array in laravel blade
(3 answers)
Closed 4 months ago.
return view($this->folder_path . 'index', compact('data' ));
I want to use foreach to display data.
array:5 [▼
0 => array:4 [▶]
1 => array:4 [▼
"id" => 2
"name" => "test"
"desc" => "my name"
"age" => 12
]
This is view where i want to display data
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item->name) }}</td>
<td>{{ $item->desc }}</td>
<td>{{ $item->age }}</td>
<tr>
#endforeach
Your $data and each $item appear to be arrays and not objects, therefore using the object accessor (->) on $item will not work. Instead you should be accessing the elements of your $item array using their key names:
#foreach($data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ ucwords($item['name']) }}</td>
<td>{{ $item['desc'] }}</td>
<td>{{ $item['age'] }}</td>
<tr>
#endforeach
Probably worthwhile doing some reading on associative arrays.

Print Array on Laravel html table

I have some data that returns to me as a laravel array and I want to print it out in a table in a view, but I can't access that data
array:1 [▼
0 => array:2 [▼
0 => array:3 [▼
0 => "MS3939"
1 => "20G1"
2 => "20DC"
]
1 => array:3 [▼
0 => "MRK940"
1 => "20R1"
2 => "20RF"
]
]
]
this is my view code with the table
<table class="table table-bordered">
<thead>
<tr class="text-center">
<th>ID</th>
<th>ISO</th>
<th>Type</th>
</tr>
</thead>
<tbody id="filter-table">
#foreach($datos as $dato)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>{{$dato['0']}}</td>
<td>{{$dato['1']}}</td>
<td>{{$dato['2']}}</td>
#endforeach
</tr>
</tbody>
</table>
If your $datos is a collection then do one thing.
$datos = $datos->flatten(1); //it will reduce one level of your array
If your $datos is not a collection and it just an array then convert it to collection
$datos = collect($datos); //then
$datos = $datos->flatten(1);
Then in view you are already running a loop, so you will be able to access data like...
<td>{{ $dato[0] }}</td>
<td>{{ $dato[1] }}</td>
<td>{{ $dato[2] }}</td>

Trying to get property of non-object from blade view in laravel

I try to display my pagination data to database ...My pagination data was from json that i already decode .....i was able to pass to my pagination data to view but not able to display in table
....
This is my error from blade view
Trying to get property of non-object
my result pagination
LengthAwarePaginator {#4177 ▼
#total: 1
#lastPage: 1
#items: Collection {#4169 ▼
#items: array:1 [▼
"data" => array:852 [▼
0 => {#624 ▶}
1 => {#623 ▶}
2 => {#628 ▼
+"ID": "cust1"
+"PWD": "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
+"NICK_NAME": "CUST1"
+"PWD_INVALID_HIT": "0"
+"PWD_CREATE_DT": "2019/07/22 15:57:15"
+"INSTITUTION_ID": "C01"
+"ACL_ID": "L04"
+"LOGIN_DT": "2019/07/22 15:57:15"
+"LOGIN_STS_ID": "N"
+"STS_ID": "C08"
+"TYPE_ID": "U00"
+"UPD_ID": "sufyzasukor"
+"UPD_DT": "2019/07/22 15:57:15"
+"EMAIL_ID": "cust1#gmail.com"
+"PHONE_NO_ID": "0"
+"HP_ID": "0 "
+"CRT_DATE_DELETED": null
+"irel__com_access_level": {#621 …9}
+"irel__com_user_types": {#630 …5}
+"irel__com_status": {#631 …5}
}
}
#perPage: "15"
#currentPage: 1
#path: "http://localhost/IFICSV3/public/configuration/comuserprofiles"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
My controller
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$total = count($content) ;
$collection = new \Illuminate\Support\Collection($content);
$paginationRecord = CollectionPaginate::paginate($collection, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
my blade table that i want to display
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user->ID }}</td>
<td>{{ $user->NICK_NAME }}</td>
<td>{{ $user->PWD_INVALID_HIT }}</td>
</tr>
#endforeach
dd($user)
array:852 [▼
0 => array:20 [▶]
1 => array:20 [▶]
2 => array:20 [▼
"ID" => "cust1"
"PWD" => "W6ph5Mm5Pz8GgiULbPgzG37mj9g="
"NICK_NAME" => "CUST1"
"PWD_INVALID_HIT" => "0"
"PWD_CREATE_DT" => "2019/07/22 15:57:15"
"INSTITUTION_ID" => "C01"
"ACL_ID" => "L04"
"LOGIN_DT" => "2019/07/22 15:57:15"
"LOGIN_STS_ID" => "N"
"STS_ID" => "C08"
"TYPE_ID" => "U00"
"UPD_ID" => "sufyzasukor"
"UPD_DT" => "2019/07/22 15:57:15"
"EMAIL_ID" => "cust1#gmail.com"
"PHONE_NO_ID" => "0"
"HP_ID" => "0 "
"CRT_DATE_DELETED" => null
"irel__com_access_level" => array:9 [ …9]
"irel__com_user_types" => array:5 [ …5]
"irel__com_status" => array:5 [ …5]
How do i display data in table
You are trying to access a array as a object . That's why the error . Try this .
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
I think it's cause you have a collection in your $user variable. You need to loop over that $user var like this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
#foreach($user as $i => $u)
<tr>
<td>{{ $u['ID'] }}</td>
<td>{{ $u['NICK_NAME'] }}</td>
<td>{{ $u['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach
#endforeach
Also, do you need this:
#php
$currentRecordno = 1;
#endphp
The $i in your foreach loop should represent the index or $currentRecordno. But if you have good reason, then stick with it.
For readability:
But really, I probably wouldn't do that extra foreach loop.
I think your controller method should be more like:
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$paginationRecord = CollectionPaginate::paginate($content['data'], count($content), '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord]);
}
Actually your blade will need to change to this:
<tbody>
#foreach($paginationRecord as $i=>$user)
#php
$currentRecordno = 1;
#endphp
<tr>
<td>{{ $user['ID'] }}</td>
<td>{{ $user['NICK_NAME'] }}</td>
<td>{{ $user['PWD_INVALID_HIT'] }}</td>
</tr>
#endforeach

Get the value of arrays in array in twig

I have this array dataPP_ids coming from a controller
array:3 [▼
0 => array:1 [▼
"plan_participate_id " => 47
]
1 => array:1 [▼
"plan_participate_id " => 47
]
2 => array:1 [▼
"plan_participate_id " => 48
]
]
I just want to show the "plan_participate_id" values in twig file.
I tried
{% for details in dataPP_ids %}
<tr>
<td class="pl-0 align-middle text-center" rowspan="2">{{ details.plan_participate_id }}</td>
<td>---</td>
</tr>
values of details.plan_participate_id are not showing in the frontend. How am I able to display it in twig?
You seem to have a trailing space here.
"plan_participate_id "
Instead of
"plan_participate_id"
You should either remove it or access it in twig like that.
{{ details['plan_participate_id '] }}

Laravel Blade Multidimensional Array

I have the below arrays and I want to get it in laravel blade as HTML table
array:2 [▼
"chest" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "1"
]
"Flys" => array:2 [▼
0 => "3"
1 => "4"
]
]
"hints" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "test1"
]
"Flys" => array:1 [▼
0 => "test2"
]
]
]
I try the below but I dont get right HTML table, its correct the first two columns but the third columnt is not, any ideas how to print it on HTML table
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th>Exercises</th>
<th>Days</th>
<th>Hints</th>
</tr>
</thead>
<tbody>
#foreach($chests['chest'] as $chest => $exc)
<tr>
<td>{{$chest}}</td>
#foreach($exc as $key => $value)
<td>
<strong>{{$value}},</strong>
</td>
#endforeach
#endforeach
#foreach($chests['hints'] as $hint => $hin)
#foreach($hin as $key => $value)
<td>
<strong>{{$value}}</strong>
</td>
#endforeach
</tr>
#endforeach
</tbody>
Answered
<tbody>
#foreach ($chests['chest'] as $exerciseName => $daysArray)
<tr>
<td>{{ $exerciseName }}</td>
#foreach ($daysArray as $day)
<td >{{ $day }}</td>
#endforeach
<td>{{ $chests['hints'][$exerciseName][0] }}</td>
</tr>
#endforeach
</tbody>
https://laracasts.com/discuss/channels/laravel/laravel-blade-multidimensional-array

Categories