1

I'm currently using Laravel 5.5 and I'm beginner. After I run my server - I get

Parse error: syntax error, unexpected 'foreach' (T_FOREACH)

Here is my index.blade.php file

<!doctype html>
<html lang="<?php echo e(app()->getLocale()); ?>">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Market</title>

</head>
<body>
    <ul>
        <?php 

            @foreach ($markets as $market) {
                echo market.name;
                <li>
                    <a href = {{ route('markets.show', $market) }}>
                        {{$market.name}}
                    </a>
                </li>
            }

         ?>
    </ul>
</body>

What should I do? I saw other similar questions - but they didn't help me.

Machavity
  • 30,841
  • 27
  • 92
  • 100

2 Answers2

1

The correct syntax for Blade template is:

<body>
    <ul>
        @foreach ($markets as $market)
            {{ $market->name }}
            <li>
                <a href = {{ route('markets.show', $market) }}>
                    {{ $market->name }}
                </a>
            </li>
        @endforeach
    </ul>
</body>

https://laravel.com/docs/5.5/blade#loops

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

Using @foreach inside PHP tags is mixing things up. Blade tags such as @foreach don't require PHP opening tags and can be inserted straight in HTML. The Blade engine will interpret them correctly.

Also:

  • Don't forget to close the @foreach call with @endforeach
  • No need to use <?php echo e(); ?> in a verbose way, Blade will output escaped content when using {{ }} tags.
  • It's recommended to put the href attribute value in double quotes.

Reformat your index.blade.php file as follows:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Market</title>

</head>
<body>
    <ul>
       @foreach ($markets as $market) {
            {{ market.name }}
            <li>
                <a href="{{ route('markets.show', $market) }}">
                    {{$market.name}}
                </a>
            </li>
       @endforeach
    </ul>
</body>

For more information about the Blade templating engine, take a look at the official docs.

Propaganistas
  • 1,674
  • 1
  • 17
  • 40