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>
Related
im having some issues with a array data that i got form a json file, but i guess the issue maybe is with the json structure that that i converted in array using json decode and file_get_contents.
Basicall this is the array strucutre:
#items: array:4 [▼
"Monday, 1 de Fev de 2021" => array:5 [▼
"PM" => array:7 [▼
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
]
"PT" => array:7 [▼
1 => "6406-2"
2 => "2976-19"
3 => "6029-8"
4 => "8130-8"
5 => "7530-8"
6 => "1071-18"
7 => "064-16"
]
"PTV" => array:7 [▶]
"PTN" => array:7 [▶]
]
"Sat, 31 de Jan de 2021" => array:2 [▼
"PTM" => array:7 [▶]
"PT" => array:7 [▶]
]
Basically the date is the table caption, and the index with the letters eg: "PTV","PM","PT"...", are the theader th titles.
And inside of the "PM" for example there are some results:
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
Where the indexes (1,2,3..) Are the prize, and the values after each index are the results.
I need to construct my table to be like this image above
This is my code:
#if(sizeof($results) > 0)
#foreach($results as $date => $result)
<div class="col-xl-12">
<table class="table">
<caption>
{{ $date }}
</caption>
<thead>
<tr>
<th id="hoje" class="tabla-header"></th>
#foreach($result as $banca => $re)
<th id="{{ $banca }}" class="tabla-header">{{ $banca }}</th>
#endforeach
</tr>
</thead>
<tbody>
{{ sizeof($result) }}
#for ($i = 0; $i < 7; $i++)
<tr>
<td>1</td>
#for ($n = 0; $n < sizeof($result); $n++)
<td>{{ $result[] }}/td>
#endfor
</tr>
#endfor
</tbody>
</table>
</div>
#endforeach
#endif
create a database and insert into table information . next with console make:controller // make a controller
make a method ...( select information of database)
$var1 = DB::table("table_name")->get('column_1');// first use DB namespace
$var2 = DB::table("table_name")->get('column_2');
$var2 = $var2[
// and other get columns table
return view('view_name',['tr1' => $var1,'tr2' => $var2 /* and etc */);
}
and view is under code
<!DOCTYPE htl>
<html>
<table class="table"> <!-- table class ; you create a table class in your code -->
<tr>
<th>{{$var1[0][0]->column_name}}</th>
<th>{{$var1[0][1]->column_name}}</th>
<th>{{$...}}</th>
</tr>
<tr>
<th> {{$avr2[0][0]->column_name}} </th>
<th> {{$var2[0][1]->coumn_name}} </th>
<!-- and continue above code -->
</tr>
</table>
</html>
first you select info of db and show them in view
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
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
I have an array that i want to return from the controller to the view.But unable to retrieve that.
If i dump my array it looks like this:
array:121 [▼
0 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "Bangalore"
"BookingId" => "EZBK000126"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "1110002223"
]
1 => array:10 [▼
"ProcedureName" => "eye"
"Status" => "referred"
"PreferredDate" => "12/12/2018"
"PreferredCity" => "mysore"
"BookingId" => "EZBK000125"
"Documents" => ""
"Name" => "vik kon"
"Age" => 62
"Gender" => "male"
"Mob" => "9146178526"
]
So i want to get it to show inside my view .Currently my code looks like this :
#foreach($new_records as $new_recordss)
<tr>
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
<td scope="col">{{ $new_recordss->Status }}</td>
<td scope="col">{{ $new_recordss->Age }}</td>
<td scope="col"><div class="btn-group">
</div>
</td>
</tr>
#endforeach
But this gives the following error:
Trying to get property of non-object
That's because an array is not an object.
// -> is for object
<td scope="col">{{ $new_recordss->ProcedureName }}</td>
// [] is for array
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
Thus this should work:
<td scope="col">{{ $new_recordss['ProcedureName'] }}</td>
<td scope="col">{{ $new_recordss['Status'] }}</td>
<td scope="col">{{ $new_recordss['Age'] }}</td>
The reason why
{{ $new_recordss->ProcedureName }}
would not work is because -> operator is used to access an Object. Notice that you get the following error:
Trying to get property of non-object
Because you have an array with 121 arrays in it. Therefore in order to access this associative array, you would use
{{ $new_recordss['ProcedureName'] }}
Here, the 'ProcedureName' is simply the key name and we are telling the array to fetch it's value. I hope this answers your question.
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