1

I don't understand how to convert my PHP array into Laravel, I am new to Laravel and can't even find exact format to follow it. I would like to use it on my blade file. Here's my php code.

$properties = array('office', 'retail');
foreach ($properties as $property) :
  echo $property;
endforeach;

And i did my best but no luck to output the correct format from Laravel

$properties = array('office', 'retail')
@foreach ($properties as $property)
  {{ $property }}
@endforeach

Thanks for the clarification.

3 Answers 3

2

Try as below :

 <?php $properties = array('office', 'retail'); ?>
 @foreach ($properties as $property)
    {{ $property }}
 @endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code and yes, i learned that it should not wrapped around the <?php ?>... Thanks for the help! Appreciate it!
2

Your code is perfect and it should work if your page is a blade i.e., Your file name should something like somepage.blade.php

You shall also add another braces

@foreach ($properties as $property)
  {{{ $property }}}
@endforeach

Update :

You should not use {{ or {{{ inside your php code !!!

You shall simply have like this

<?php
$properties = array('office', 'retail')
?>
@foreach ($properties as $property)
  {{{ $property }}}
@endforeach

5 Comments

still throws an error on my project (Whoops, looks like something went wrong). I can't guess what was the issue and does the array line correct? i did follow the one on the net, but still have an error; $array = array_add($array, 'key', 'value')
Your file name contains is a blade right ? i.e., yourfile.blade.php ?
Instead of {{ $property can you just do this ? @foreach ($properties as $property) test @endforeach ?
I guess you are using {{ inside php.. You should not do it ..... !! I have updated the answer, Please check it
ok, i think i got the idea... it is working now... tried Ankii Gangrade answer and it works... It should not wrapped around <?php ?>...
1

Its important to first understand the working of blade control structures. Laravel 5 provides many control structures for working with blade files.

Echoing Data

Hello, {{ $name }}.

The current UNIX timestamp is {{ time() }}.

Echoing Data After Checking For Existence

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:

{{ isset($name) ? $name : 'Default' }}

However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:

{{ $name or 'Default' }}

Displaying Raw Text With Curly Braces

If you need to display a string that is wrapped in curly braces, you may escape the Blade behavior by prefixing your text with an @ symbol:

@{{ This will not be processed by Blade }}

If you don't want the data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

Docs help

For more detail you can read the laravel docs about Laravel Templates

Underlying Concepts

So basically Laravel gives you a pretty and safe way of displaying the dynamic data. The blade code is parsed by the Laravel templating engine and replaced with relevant PHP code. So if you wrapping the blade code in <?php ?> the Laravel will be unable to parse it and will crash.

Analogy For Wrong Code

Its like you have wrapped the HTML code in <?php ?> and expecting the HTML engine will somehow render it. Hope this will clarify your underlying blade concepts.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.