1

I have an object ($project) in Blade.

When I do a {{ var_dump($project) }} I get:

object(stdClass)[603]
  public 'image_header' => 
    array (size=1)
      0 => 
        object(stdClass)[610]
          public 'img' => string 'amsterdam.jpg' (length=13)

When I try to access this element in blade with:

{{ $project->image_header[0]->img }}

It keeps giving this error:

Undefined offset: 0.

Print_r output:

stdClass Object ( 
           [image_header] => Array ( 
                             [0] => stdClass Object ( 
                                       [img] => amsterdam.jpg 
                                    ) 
                             ) 
                 )
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
klaaz
  • 551
  • 8
  • 20

1 Answers1

0
//recreating your original
$obj2 = (object)['img' => 'amsterdam.jpg'];
$obj1 = (object)['image_header' => [$obj2]];

$answer = json_decode(json_encode($obj1), true)['image_header'][0]['img'];

var_dump($answer); //string(13) "amsterdam.jpg" 

From stackoverflow back in 2010 (requires PHP >= 5.2 for native json and 5.? to get the elements from the function call, else split into two lines)

Community
  • 1
  • 1