Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

laravel

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

?

like image 961
Inigo Avatar asked Jan 24 '14 17:01

Inigo


People also ask

Can we write PHP code in blade template Laravel?

Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

How do you echo in Blade file?

By default you would use the following syntax {{ $some_variable }} to echo out the content of a specific variable in Blade. By default the {{ }} escapes the HTML tags.

Why does Laravel use the blade template engine?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .

What is Blade templating in Laravel?

Blade Templating Blade is a simple, yet powerful templating engine provided with Laravel. Unlike controller layouts, Blade is driven by template inheritance and sections. All Blade templates should use the . blade. php extension.


1 Answers

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.

like image 59
Antonio Carlos Ribeiro Avatar answered Nov 02 '22 10:11

Antonio Carlos Ribeiro