Laravel Parse error on @foreach

Dan

I am following the video Laravel 5.4 From Scratch: Eloquent Relationships and Comments and have this error:

Parse error: syntax error, unexpected ')', expecting '['

I am getting it when I try to replicate the video and put:

@foreach ($film->comment as comment)

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

@endforeach

This is the section of code being highlighted on the error page:

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

However when I output just {{$film->comments}} it does give me a result:

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

I have a film table and comment table. In tinker it displays as I want e.g.

>>> $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,    }

Below are my models:

Comment Model:

class Comment extends Model

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

Film Model:

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

Any help much appreciated!

Dan
@foreach ($film->comments as $comment)
<p>{{$comment->body}}</p>
@endforeach

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel's Foreach syntax error

syntax error, unexpected ')' foreach laravel

PHP Parse error: syntax error, unexpected '?' - laravel

(Laravel) Parse error: syntax error, unexpected '<'

Getting weird undefined foreach error in Laravel Blade

Laravel Carbon failed to parse time string error

Laravel "Parse error" after clean Install

parse error when trying to display in laravel view

syntax error, unexpected 'foreach' (T_FOREACH) using laravel 6

Laravel 5.2 artisan error: Parse error: syntax error, unexpected ':'

Laravel : Parse error: syntax error, unexpected 'if' (T_IF) on view

How to fix PHP Parse error: syntax error, unexpected '?', on laravel 5.8

Laravel 4 - foreach in foreach

PHP / Laravel - Foreach store into database (\Grammar::parameterize() error)

What is the "Malformed @foreach statement." error on Laravel's Blade?

Laravel 4 foreach error - Trying to get property of non-object

Loop through error messages without withErrors($validator) in Laravel - foreach

Laravel giving error in foreach loop in spatie-permission

Symfony parse error in output.php when creating project in laravel

Parse error after deploy Laravel 5.1 on shared hosting

Parse error in Artisan when running "laravel new appName"

Parse error: Invalid numeric literal after upgrade to laravel 5.2

How may I avoid a syntax error when echoing JSON.parse in a script in PHP foreach?

Laravel - Parse error in /laravel/framework/src/Illuminate/Foundation/helpers.php

Laravel 5 : Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE)

laravel "Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE)"

Laravel parse error: syntax error, unexpected T_CLASS, expecting T_STRING

php laravel Parse error: syntax error, unexpected 'return' (T_RETURN)

Php error : parse error