Nested foreach in Laravel Blade - php

I am new to Laravel and I am trying to display data from the controller to view. I am able to display data from the first level of #foreach( $item->name, $item->address, etc) with ease. But I am having difficulty accessing a child of "subscriber_subscription" via Laravel blade convention. I tried accessing it using via code below:
#foreach ($items as $item)
...
#foreach($item->subscriber_subscription as $subs)
...
...
#endforeach
...
...
#endforeach
JSON data used is:
[
{
"id":1,
"name":"Jollibee",
"address":"31st cor. 9th Strata bldg. Ortigas Ave. Pasig City, 600",
"contact":"87000",
"email":"admin#jollibee.com.ph",
"created_at":"2017-09-17 01:04:11",
"updated_at":"2017-09-17 01:04:12",
"subscriber_code":"jollibee",
"subscriber_subscription":[
{
"id":1,
"subscriber_id":1,
"subscription_id":1,
"start_date":"2017-09-17 01:35:27",
"end_date":"2018-09-17 01:35:27",
"created_at":"2017-09-17 01:35:23",
"updated_at":"2017-09-17 01:35:23"
},
{
"id":6,
"subscriber_id":1,
"subscription_id":1,
"start_date":"2017-10-17 01:35:27",
"end_date":"2018-10-17 01:35:27",
"created_at":"2017-09-17 01:35:23",
"updated_at":"2017-09-17 01:35:23"
}
]
},
{
"id":2,
"name":"Bench",
"address":"2207 Tektite Bldg. Ortigas Ave. Pasig City, 1600",
"contact":"+639171234567",
"email":"admin#benchtm.com.ph",
"created_at":"2017-09-17 01:47:10",
"updated_at":"2017-09-17 01:47:11",
"subscriber_code":"bench",
"subscriber_subscription":[
{
"id":4,
"subscriber_id":2,
"subscription_id":1,
"start_date":"2017-09-17 14:19:15",
"end_date":"2018-09-17 14:19:16",
"created_at":"2017-09-17 14:19:20",
"updated_at":"2017-09-17 14:19:20"
},
{
"id":5,
"subscriber_id":2,
"subscription_id":2,
"start_date":"2017-09-17 14:19:15",
"end_date":"2018-09-17 14:19:16",
"created_at":"2017-09-17 14:19:20",
"updated_at":"2017-09-17 14:19:20"
}
]
}
]
I'm getting this error: Invalid argument supplied for foreach()
Any help will be appreciated. Peace :-)
Thanks

I've solved it. Apparently, i have to use $item->SubscriberSubscription which is the name of my model

Related

API Platform return deep association data

I would like to request data from API Platform such as I can get a list of book categories when I request a list of shops.
So I have 3 entities with ManyToMany associations: Shop <--> Book <--> Category
When I call the GET /shops endpoint, I'd like to get a list of shops and for each shop, a list of categories (depending on the books available in the shop). Something like:
{
"#context": "/contexts/Shop",
"#type": "hydra:Collection",
"hydra:member": [
{
"#id": "/shops/1",
"#type": "Shop",
"name": "Shop 1",
"categories": [
{
"id": "/categories/12"
"#type": "Category"
"name": "Novel"
},
{
"id": "/categories/15"
"#type": "Category"
"name": "SF"
}
]
}
]
}
For now, I did it with a calculated field on the Shop entity:
#[Groups(['shop:collection:get'])]
public function getCategories(): array
{
$categories = [];
foreach ($this->getBooks() as $book) {
foreach ($book->getCategories() as $category) {
$categories[$category->getId()] = $category;
}
}
return array_values($categories);
}
I also created a Doctrine Extension to add joins to the SQL query when I request a collection of shops to improve performance but I am not sure if that's a good practice...
Is there any other way to get the same JSON result?
I also tried to use addSelect() with the QueryBuilder inside the Doctrine Extension but then the shop data are stored as a sub object:
{
"#context": "/contexts/Shop",
"#type": "hydra:Collection",
"hydra:member": [
{
"0": {
"#id": "/shops/1",
"#type": "Shop",
"name": "Shop 1",
}
}
]
}

Filter relationship, based on array of integers

My model has many options, multiple fields of a single option store values as an array on integers.
I want to filter based on an integer is included.
in php similare to in_array(), but the array is stored in db, and the number is key word.
$productsWithOptions = [
{
"id":2,
"name":"mac 2",
"options":[
{
"id":4,
"price":99.9,
"product_id":2,
"colors":"[4,3,2]",
"sizes":"[5,3,2]"
},
{
"id":5,
"price":99.9,
"product_id":2,
"colors":"[4,4,5]",
"sizes":"[1,2,2]"
},
{
"id":7,
"name":"mac 7",
"options":[
{
"id":19,
"price":99.9,
"product_id":7,
"colors":"[6,3,4]",
"sizes":"[5,2,5]"
},
{
"id":20,
"price":1999.9,
"product_id":7,
"colors":"[1,6,6]",
"sizes":"[5,6,2]"
},
{
"id":21,
"price":1999.9,
"product_id":7,
"colors":"[4,6,5]",
"sizes":"[5,2,2]"
}
]
}
]
$products = Product::whereHas('options', function (Builder $query) use ($arr) {
$query->where('colors','like', "%2%" );
})->get();
This is how options table looks like in db.

Accessing Data in Array

I retrieved data from my database like below and the array response. I am trying to access products and display the pids. How can get this done.
I retrieved data from my database like below and the array response. I am trying to access products and display the pids. How can get this done.
PS: Beginner with laravel. Please help
$amazon_products = Group::where('id',$auth_data)->with('products')->get();
return $amazon_products
[
{
"id":1,
"title":"Shipment",
"id":1,
"created_at":"2018-04-15 23:55:00",
"updated_at":"2018-04-16 00:10:52",
"products":[
{
"id":3,
"name":"MacBook",
"pid":"050",
"group_id":null,
"user_id":1,
"created_at":"2018-04-16 11:06:08",
"updated_at":"2018-04-16 11:06:08",
"pivot":{
"category_id":1,
"product_id":3,
"created_at":"2018-04-16 11:06:08",
"updated_at":"2018-04-16 11:06:08"
}
}
]
},
{
"id":2,
"title":"Gaming",
"id":1,
"created_at":"2018-04-15 23:55:30",
"updated_at":"2018-04-15 23:55:30",
"products":[
{
"id":1,
"name":"Samsung",
"pid":"024",
"group_id":null,
"user_id":1,
"created_at":"2018-04-16 00:14:20",
"updated_at":"2018-04-16 05:31:05",
"pivot":{
"group_id":2,
"customer_id":1,
"created_at":"2018-04-16 05:33:08",
"updated_at":"2018-04-16 05:33:08"
}
}
]
}
]
Below is the model for my group
Group
public function products()
{
return $this->belongsToMany('App\Product','group_product','group_id','product_id')
->withTimestamps();
}
List the product pids after eager loading the products:
...->get()->load('products')->lists('products.pid');

Using php foreach to get a second JSON Nested Array

I have a nested array from a json API that usually have zero, one, or two arrays.
I can get it so if the array is empty to display "N/A", and to display only the first ([0]) instance of the array.
However displaying the second array is tripping me up. I know I need to use the foreach to iterate through the PosTags (see below) array, so to extract each Tag value in the body of the foreach loop. However, I can't figure HOW to do this correctly.
My JSON:
[
{
"SectionDetails": [
{
"Description": "Course Description",
"Departments": "Department",
"Credits": "3.00",
"Meetings": [
{
"DOW": "Th",
"Dates": "08-31-2017 to 12-08-2017",
"Times": "03:00 PM - 05:30 PM",
}
],
"PosTags": [
{
"Tag": "HART-ANC"
},
{
"Tag": "ARCH-ARCH"
}
]
}
]
}
]
Current PHP:
$postag = "N/A";
if (isset($sectiondetails->{'PosTags'}))
{
if(!empty($sectiondetails->{'PosTags'})) {
$postag=$sectiondetails->{'PosTags'}[0]->{'Tag'};
}
}
Running a foreach loop like the following displays "Object of class stdClass could not be converted to string" on the line where echo tag is:
if (isset($sectiondetails->{'PosTags'})) {
if(!empty($sectiondetails->{'PosTags'})) {
$postag=$sectiondetails->{'PosTags'};
foreach ($postag as $tag) {
echo $tag;
}
}
}
Ideally I'd like HART-ANC, ARCH-ARCH to be displayed. Thoughts?

Laravel Blade echoing properties of child...properties?

SETUP
Model
public function totalValidated()
{
return $this->hasOne('user')->where('validated', '=', "1")
->selectRaw('district_id, count(*) as total')->groupBy('district_id');
}
Controller
$districts = District::select(array('id', 'name'))
->with(array('usersCount','totalValidated'))
->get();
return View::make('districts.index', compact('districts'));
I have the following data returned:
0: {
id: 1,
name: "Northern California",
total_validated: {
district_id: "1",
total: 3
}
},
1: {
id: 2,
name: "Southern California",
total_validated: {
district_id: "2",
total: 30
}
}
View
#foreach ($districts as $district)
<tr>
<td>{{$district->name}}</td><td>{{$district->total_validated->total}}<td>
<tr>
#endforeach
Question
How do i display the "total" in "total_validated" per district? The above only gets me a
"Trying to get property of non-object"
which i know is wrong, but i'm not sure how to get the value out of that nested array.
Thanks!
Not sure what's up with blade, but plain php worked
<?php echo($district['totalValidated']['total']) ?>

Categories