I'm trying to show JSON data from this https://pomber.github.io/covid19/timeseries.json, but I got an error:
(ErrorException(code: 0): Undefined index: confirmed
What I expect is I can show the list of Country Name following with the Date, Confirmed, etc.
Here's my view:
#foreach($results as $json_d)
{{ $json_d['date'] }}
{{ $json_d['confirmed'] }}
{{ $json_d['deaths'] }}
{{ $json_d['recovered'] }}
#endforeach
And here's my controller:
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$results = json_decode($response, true);
return view('dashboard', compact('results'));
Any help would be appreciated :)
That's because you have to do it as a nested for. As the answer of Dino, but there's the way of get the country key.
#foreach($results as $key => $val)
Data for the country: {{ $key }}
#foreach(((array)$results)[$key] as $data)
{{ $data['date'] }}
{{ $data['confirmed'] }}
{{ $data['deaths'] }}
{{ $data['recovered'] }}
#endforeach
#endforeach
The result you get with Guzzle is a list of countries
dd($results);
gives
array:152 [▼
"Thailand" => array:57 [▶]
"Japan" => array:57 [▶]
"Singapore" => array:57 [▶]
"Nepal" => array:57 [▶]
"Malaysia" => array:57 [▶]
"Canada" => array:57 [▶]
"Australia" => array:57 [▶]
"Cambodia" => array:57 [▶]
"Sri Lanka" => array:57 [▶]
"Germany" => array:57 [▶]
"Finland" => array:57 [▶]
...
which means you need another loop
#foreach($results as $countryData)
#foreach($countryData as $data)
{{ $data['date'] }}
{{ $data['confirmed'] }}
{{ $data['deaths'] }}
{{ $data['recovered'] }}
#endforeach
#endforeach
Related
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 want to get all data in a loop. Since I am new to in Laravel i do not know how to do it. Thanks in advance.
My Array looks like this:
array:2 [
0 => Collection {
#items: array:1 [
0 => {
"id": 3
"gallery_id": 1
"is_thumb_image": 1
"thumbnail": "public\album/kRzunwjAEIXciv1nN2RdsUFB4Ptpu4TjAUSECZCE.jpeg"
}
]
}
1 => Collection {
#items: array:1 [
0 => {
"id": 6
"gallery_id": 2
"is_thumb_image": 1
"thumbnail": "public\album/BKYp72YUZNiZdljDyOjny9HgikR0whlyp0dkLVzV.jpeg"
}
]}]
You should try this:
If you use in blade file then below answer helpful you
#foreach($collections as $collection)
#foreach($colection as $item)
{{ $item->id; }}
{{ $item->is_thumb_image; }}
#endforeach
#endforeach
Use foreach loop to display the data
ex:
#foreach($collections as $collection)
#foreach($colection as $item)
{{ $item->id; }}
{{ $item->is_thumb_image; }}
#endforeach
#endforeach
I am looking to pass in a collection of Board objects into my view. However, it fails on the for loop inside the blade template.
This is the route to the view
Route::get('board', function () {
$user = Auth::user();
$board = Auth::user()->boards()->where('user_id', $user->id)->first();
$boardColumns = $board->columns()->get();
return view('pages.kanban', compact('user', 'board', 'boardColumns'));
});
This is the result of dd method
Collection {#299 ▼
#items: array:4 [▼
0 => BoardColumn {#300 ▶}
1 => BoardColumn {#301 ▶}
2 => BoardColumn {#302 ▶}
3 => BoardColumn {#303 ▶}
Finally, this is the for each loop
#foreach($boardColumns as $column)
<li class="drag-column {{ $column->slug }}">
<div class="drag-column-header">
<h2>{{ $column->name }}</h2>
</div>
<div class="drag-options" id="options{{ $loop->iteration }}"></div>
<ul class="drag-inner-list" id="{{ $loop->iteration }}">
#foreach($column->tasks as $task)
<li class="drag-item"></li>
#endforeach
</ul>
</li>
#endforeach
I have the following array result set, I'm trying to loop through each of the results and just echo them out onto the page. I'm using Laravel 5.2 and the blade templating engine
Collection {#240 ▼
#items: array:3 [▼
0 => array:2 [▼
"name" => "desktop"
"views" => "349"
]
1 => array:2 [▼
"name" => "mobile"
"views" => "151"
]
2 => array:2 [▼
"name" => "tablet"
"views" => "68"
]
]
}
This is what I have so far
#foreach($devices as $device)
$key = 0; $key++; $key < 2;
{{ $device[$key] }},
#endforeach
#foreach($devices as $device)
{{ $device->name }}
{{ $device->views}}
#endforeach
Will be enough.
You need to echo object properties:
#foreach($devices as $device)
{{ $device->name }} has {{ $device->views }}
#endforeach
If you like to use key then
#foreach($devices as $key => $val)
{{ $device[$key]->name }},
{{ $device[$key]->views }}
#endforeach
Why is my foreach loop fails with error "Trying to get property of non-object":
#foreach ($memberships as $membership)
{{ $membership->id }}
#endforeach
but this works just fine:
#foreach ($memberships as $membership)
<?php print_r($membership['id']); ?>
#endforeach
if I dd($memberships); I get
array:2 [▼
0 => array:1 [▼
"id" => 8
]
1 => array:1 [▼
"id" => 9
]
]
As was pointed out, I was trying to access array as an object...
#foreach ($memberships as $membership)
{{ $membership['id'] }}
#endforeach