@foreach

时间:2018-01-11 01:37:39

标签: php laravel sqlite foreach parse-error

我正在关注视频Laravel 5.4 From Scratch: Eloquent Relationships and Comments并出现此错误:

  

解析错误:语法错误,意外')',期待' ['

当我尝试复制视频并将其放入时,我得到了它:

@foreach ($film->comment as comment)

<p>{{$comment->body}}</p>

@endforeach

这是错误页面上突出显示的代码部分:

<?php $__currentLoopData = $film->comment; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as comment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

然而,当我只输出{{$film->comments}}时,它确实给了我一个结果:

[{"id":1,"film_id":"1","body":"I loved this film!","created_at":null,"updated_at":null}]

我有一张电影桌和评论表。在修补程序中它会显示我想要的内容。

>>> $c = App\Comment::first();
=> App\Comment {#751
     id: "1",
     film_id: "1",
     body: "I loved this film!",
     created_at: null,
     updated_at: null,    }
>>> $c->film;
=> App\Film {#746
     id: "1",
     title: "The Godfather",
     director: "Francis Ford Coppola",
     description: "It stars Marlon Brando and Al Pacino as the leaders of a fictional New York crime family. The story, spanning 1945 to 1955, chronicles the family under the patriarch Vito Corleone (Brando), focusing on the transformation of Michael Corleone (Pacino) from reluctant family outsider to ruthless mafia boss.",
     date: "1972-03-24",
     created_at: null,
     updated_at: null,    }

以下是我的模特:

评论模型:

class Comment extends Model

{
    public function film()
    {
        return $this->belongsTo(Film::class);
    }
}

电影模特:

class Film extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

List<string> strList = new List<string>();
foreach(char x in charList)
{
    strList.Add(x.ToString());
}

答案 1 :(得分:0)

首先确保添加

的$ film-&gt;评论不为空
@if( count($film->comments) > 0 ) 
   @foreach($film->comments as $comment)
      <p>{{$comment->body}}</p>
   @endforeach
@endif