解析错误:语法错误,意外的“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
Parse error: syntax error, unexpected 'foreach' (T_FOREACH) [LARAVEL]?
提问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>
回答by Propaganistas
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.
@foreach
在 PHP 内部使用标签会混淆。Blade 标签,例如@foreach
不需要 PHP 开始标签,可以直接插入到 HTML 中。Blade 引擎将正确解释它们。
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.
- 不要忘记结束
@foreach
通话@endforeach
- 无需
<?php echo e(); ?>
冗长使用,Blade 会在使用{{ }}
标签时输出转义内容。 - 建议将
href
属性值放在双引号中。
Reformat your index.blade.php
file 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 模板引擎的更多信息,请查看官方文档。