mercredi 21 août 2019

Eloquent resulting single search result as array not collection

I have some instances where a eloquent is resulting an single array not a collection. Although dd shows it as a collection with a single entry.

For example I have a query in a controller:

$pg = Page::with('getPanels')->where('slug',$slug)->get();

This will return a single result and works fine, so I pass this to a blade template. My complete function is

$pg = Page::with('getPanels')->where('slug',$slug)->get();
return view('front.page',['pg' => $pg]);

As soon as he template is brought in it will fall over at

if (!is_null($pg->headImage))

{$img = asset('images/pages')."/".$pg->headImage;}

and I will get

Property [headImage] does not exist on this collection instance.

If I change the line to

if (!is_null($pg[0]['headImage']))

it will continue OK. This is of course a pain as I would much rather use $pg->headImage.

Can someone enlighten me please?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2MvEALU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire