解析错误:语法错误,意外的“foreach”(T_FOREACH)[LARAVEL]?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46981651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:52:37  来源:igfitidea点击:

Parse error: syntax error, unexpected 'foreach' (T_FOREACH) [LARAVEL]?

phplaravellaravel-5

提问by

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

我目前正在使用Laravel 5.5,我是初学者。在我运行我的服务器后 - 我得到

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

解析错误:语法错误,意外的“foreach”(T_FOREACH)

Here is my index.blade.php file

这是我的 index.blade.php 文件

<!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.

我该怎么办?我看到了其他类似的问题 - 但他们没有帮助我。

采纳答案by Alexey Mezenin

The correct syntax for Blade template is:

Blade 模板的正确语法是:

<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

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

回答by Propaganistas

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

@foreach在 PHP 内部使用标签会混淆。Blade 标签,例如@foreach不需要 PHP 开始标签,可以直接插入到 HTML 中。Blade 引擎将正确解释它们。

Also:

还:

  • Don't forget to close the @foreachcall 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 hrefattribute value in double quotes.
  • 不要忘记结束@foreach通话@endforeach
  • 无需<?php echo e(); ?>冗长使用,Blade 会在使用{{ }}标签时输出转义内容。
  • 建议将href属性值放在双引号中。

Reformat your index.blade.phpfile as follows:

index.blade.php按如下方式重新格式化您的文件:

<!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.

有关 Blade 模板引擎的更多信息,请查看官方文档