Foreach with a multidimensional array - Laravel Blade templating - php

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

Related

Laravel: parse JSON to view

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

How to parse the value in blade laravel?

Parse the laravel array data Am tried not show any data?
Please suggest to any other source?
here is the my dd Values in my laravel controller
array:2 [▼
"count" => 5
"date" => array:1 [▼
0 => array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
]
]
am unable to parse the value on blade my tried
{{ $nov['date']['0']['0'] }} ---- > here i get the exact value "2019-11-04 15:41:53" i need parse in dynamic data
#foreach ($nov as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach #endforeach
Thanks
If you want to loop just the index 0 of date
#foreach ($nov['date']['0'] as $key => $value)
{{ $value}}
#endforeach
If you want to loop all the date
#foreach ($nov['date']['0'] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach
#endforeach
All the dates are in the key of $nov['date'][0]. So you've to make a loop by using this key instead of $now.
Change in foreach loop and use $nov['date'][0] instead of direct $nav
#foreach ($nov['date'][0] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach
if you print_r($now['date']) you'll get
0 => array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
and then if you access 0 index like $now['date'][0] then you will get the following data which you need to make a loop.
array:5 [▼
0 => "2019-11-04 15:41:53"
1 => "2019-11-05 14:28:10"
2 => "2019-11-12 13:47:31"
3 => "2019-11-14 12:39:12"
4 => "2019-11-17 10:54:39"
]
Hope you understand.
Use this one.
#foreach ($nov['date'] as $key => $value)
#foreach ($value as $key => $da)
{{ $da }}
#endforeach #endforeach

Laravel : How to get collection of array data in loop

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

Laravel 5 Loop Through Array

I am passing a variable $mailchimp from my Controller to my View.
this is what I got with {{dd($mailchimp)}}
array:8 [▼
"id" => "xyz123"
"email_address" => "john.doe#discworld.com"
"unique_email_id" => "c9a36649c8"
"email_type" => "html"
"status" => "subscribed"
"merge_fields" => array:2 [▼
"FNAME" => "John"
"LNAME" => "Doe"
]
"stats" => array:2 [▼
"avg_open_rate" => 0
"avg_click_rate" => 0
]
"list_id" => "769808qeqw92"
]
how can I loop through this array ($mailchimp) ? With the code below I get an exception: "htmlentities() expects parameter 1 to be string, array given"
#foreach($mailchimp as $user)
#if(is_array($user))
#foreach($user as $key => $value)
{{$value}}
#endforeach
#endif
#endforeach
Update:
With this Code in My Controller
public function index()
{ //Fetch all subscribers from DB
$subscribers = Subscriber::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->get();
foreach ($subscribers as $key => $subscriber) {
//Check if the local subscriber is also present in mailchimp
$mailchimp = Newsletter::getMember($subscriber->email);
}
return view('backend.newsletter.contacts.index')->withSubscribers($subscribers)
->withMailchimp($mailchimp);
}
I need to iterate the mailchimp array. As there are multiple users, alexey's suggestion doesn't work out anymore.
This stil doesn't work:
#foreach($mailchimp as $key => $user)
{{$user}}
#endforeach
You don't need to iterate over $user. If $mailchimp is an array of users, do this:
{{ $mailchimp['email_adress'] }}
{{ $mailchimp['merge_fields']['FNAME'] }} {{ $mailchimp['merge_fields']['LNAME'] }}
Since you are only interested in printing the values in your array, you can use array_flatten to get rid of the nested arrays, and then loop through the result:
#foreach(array_flatten($mailchimp) as $userData)
{{$userData}}
#endforeach

Using foreach on array in Laravel view

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

Categories