1

I am trying to associate a user to a comment. I am able to associate the correct comments to each film. If I use {{$comment->user_id}} it will display the user id next to the comment but it is getting this information from the comments table. I wish to display the name of the user which is found in the user table.

My comments table:

Schema::create('comments', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->integer('film_id');
    $table->string('body');
    $table->timestamps();

My users table:

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

My comment class:

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

    }

    public function user()
    {
        return $this->belongsTo(User::class);

    }

My user class

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

My controller:

/* Displays list of films*/

        public function display()
            {
                $films = Film::all();
                return view('list', compact('films'));
            }

/* Displays comments for a film*/

            public function comment($id)
            {
                return view('comment', ['film' => Film::find(1)]);
            }

I thought maybe I could pass another argument in the comment function like this:

public function comment($id)
{
      $user = User::find($id)
    return view('comment', ['film' => Film::find(1)], ['user' => $user]);

But that does not work. I get the below error. Though I know its a syntax error I think it's not something I can do.

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

Dan
  • 951
  • 1
  • 23
  • 46

1 Answers1

1

You forgot to put ; after User::find($id), that's why you're getting this error.

You can also use this syntax:

public function comment($id)
{
    return view('comment', [
        'film' => Film::find(1),
        'user' => User::find($id)
    ]);
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279