Inigo
Inigo

Reputation: 8685

Laravel - trying to echo a value from an array in a blade template. Works using php echo() but not blade syntax

I have an array that looks like:

$myArray = array(
           'firstRow' => array(
                0 => array(
                      'id' => 1
                      'title' => 'First Cat.'
                      ),
                1 => array(
                      'id' => 2
                      'title' => 'Second Cat.'
                      )
                    ),
           'SecondRow' => array(
                0 => array(
                      'id' => 3
                      'title' => 'Third Cat.'
                      ),
                1 => array(
                      'id' => 4
                      'title' => 'Fourth Cat.'
                      )
                    )
            );

This is being passed to my blade template. I can echo out values using raw php like:

<?php echo $myArray['firstRow'][0]['title'] ?>

Which works as expected. However, when I try to do what I thought was exactly the same thing using blade's syntax:

{{ $myArray['firstRow'][0]['title'] }}

I get the error:

Trying to get property of non-object

?

Upvotes: 5

Views: 14171

Answers (2)

Inigo
Inigo

Reputation: 8685

OK.. I just solved this. Stupid. Sorry and thanks to the other commenters here.

The problem was that I had come code underneath this line which was commented out using HTML comments, ie <-- --> But it also contained blade syntax which of course is PHP and so is still firing. I would normally have noticed but due to my GUI not highlighting blade syntax it went unnoticed.

Upvotes: 3

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

I'm afraid you are suspecting of the wrong line of code, because:

Trying to get property of non-object

Is to something being used not as an array, but as an object:

{{ $myArray->firstRow->get(0)->title }}

So, your error is not exactly in this line.

But can be sure by getting the generated view source code in app/storage/views.

Upvotes: 4

Related Questions