Reputation: 389
I have a model with several fields, including two text fields that store JSON lists of dictionaries. One of them, that stores image data, is working fine; however a second one that stores a list of dicts with links returns a Undefined property: stdClass::$title
when I try to access the property from a blade template.
All of the other properties (including the images JSON converted to an array of objects) are rendering fine if I remove the call to my links property.
I've tried to dd()
the links property and it both shows that it's set, it's an array, and it's full of objects with both the properties (title, url) that I'm trying to access when it fails.
Once I try to actually access them, however, I get that Undefined property
for the exact properties I'm trying to access.
Wondering if anyone has encountered anything like this? The really odd thing is that the images JSON data is rendering without a single problem. It's all tied together with Route Model binding, which is verified to be working.
public function getLinksAttribute() {
if (!empty($this->attributes['links'])) {
return json_decode($this->attributes['links']);
}
}
public function getImagesAttribute() {
if (!empty($this->attributes['images'])) {
return json_decode($this->attributes['images']);
}
}
@if (is_array($artist->links))
<div class="links">
<h4>Links</h4>
<ul>
@foreach ($artist->links as $link)
{{ $link->title }}, {{ $link->url }}
@endforeach
</ul>
</div>
@endif
@if (is_array($artist->images))
<ul class="images">
@foreach ($artist->images as $image)
<li>{!! Html::image(Html::buildS3Url(array(
"basedir" => "artists", "id" => $artist->id, "prefix" => $image->prefix,
"extension" => $image->extension, "conversion" => "display")
), $artist->name) !!}</li>
@endforeach
</ul>
@endif
dd()
### links (doesn't work)
#tinker output
links: "[{"'title'":"test","'url'":"http:\/\/test.com"}]",,
# dd()
array:1 [▼
0 => {#308 ▼
+"'title'": "test"
+"'url'": "http://test.com"
}
]
### images (works)
# tinker output
images: "[{"prefix":1440693993,"extension":"png"},{"prefix":1440697822,"extension":"png"}]"
# dd()
array:2 [▼
0 => {#308 ▼
+"prefix": 1440693993
+"extension": "png"
}
1 => {#307 ▼
+"prefix": 1440697822
+"extension": "png"
}
]
Upvotes: 1
Views: 4133
Reputation: 19372
"Undefined property: stdClass::$title"
seems like in one of Your links missing title property
You can check it by:
@foreach ($artist->links as $link)
<?php if(property_exists($link, "title")) : ?>
{{ $link->title }}
<?php else : ?>
NO TITLE [DEBUG: {{ dd($link) }}]
<?php endif; ?>, {{ $link->url }}
@endforeach
also I've found one thing:
links: "[{"'title'":"test","'url'":"http:\/\/test.com"}]",,
Your element field is ' title ' (with '), but it must be "title": "test"
You can fix it by removing single quotes in paramater names.
Upvotes: 3