Php @foreach上的Laravel分析错误
我正在跟踪视频,出现以下错误: 分析错误:语法错误,意外“'),应为“[” 当我尝试复制视频并放置以下内容时,我得到了:Php @foreach上的Laravel分析错误,php,laravel,sqlite,foreach,parse-error,Php,Laravel,Sqlite,Foreach,Parse Error,我正在跟踪视频,出现以下错误: 分析错误:语法错误,意外“'),应为“[” 当我尝试复制视频并放置以下内容时,我得到了: @foreach ($film->comment as comment) <p>{{$comment->body}}</p> @endforeach 以下是我的模型: 注释模型: class Comment extends Model { public function film() { return
@foreach ($film->comment as comment)
<p>{{$comment->body}}</p>
@endforeach
以下是我的模型:
注释模型:
class Comment extends Model
{
public function film()
{
return $this->belongsTo(Film::class);
}
}
电影型号:
class Film extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
非常感谢您的帮助!@foreach($film->comments as$comment)
@foreach ($film->comments as $comment)
<p>{{$comment->body}}</p>
@endforeach
{{$comment->body}
@endforeach
首先确保添加时$film->comments不为空
@if( count($film->comments) > 0 )
@foreach($film->comments as $comment)
<p>{{$comment->body}}</p>
@endforeach
@endif
@if(计数($film->comments)>0)
@foreach($film->comments as$comment)
{{$comment->body}
@endforeach
@恩迪夫
试试@foreach($film->comment as$comment)
注意注释上的$
。当我这样做时,它说的是无效的论点,我认为你在打字:循环$film->comments
而不是$film->comment
,很抱歉我一开始就错过了。是的,这样做很有效:$film->comments as$film->comment和段落中的$film->comment->body。不确定为什么在t中不是这样他看了吗?非常感谢!你们想把答案写出来,这样我就可以给你们打分:)也许@foreach($film->comments as$comment)
感谢您提供这段代码片段,它可能会提供一些有限的、即时的帮助。A通过说明这是一个很好的问题解决方案的原因,并使它对将来有其他类似问题的读者更有用。请在您的回答中添加一些解释,包括您所做的假设。
@foreach ($film->comments as $comment)
<p>{{$comment->body}}</p>
@endforeach
@if( count($film->comments) > 0 )
@foreach($film->comments as $comment)
<p>{{$comment->body}}</p>
@endforeach
@endif