1
votes

I am learning Laravel and there is one thing I cannot solve.

What do I have:

Table of Users, Posts, Comments and corresponding Models

User.php:

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

public function publishComment(Comment $comment)
    {
        $this->comments()->save($comment);
    }

Post.php:

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

Comment.php:

public function post() { return $this->belongsTo(Post::class); } 

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

What do I want?

Since I have Blog, one logged user can create many posts (this works). But same logged user can create many comments to one post. (User has many comments, post has many comments).

Tables:

User: | id | name | email | password

Comment: | id | user_id | post_id | body

Post: | id | user_id | title | body

What have I tried? I created controller

CommentsController:

class CommentsController extends Controller

{

public function store(Post $post)
{

    $this->validate(request(), ['body' => 'required|min:2']);

    auth()->user()->publishComment(

        new Comment(request('body'));

    );

    return back();

}

And in my blade file I simply want $comment->user->name (get name of user who does the comment belongs to)

Error I get: Trying to get property of non-object

<?php echo e($comment->user->name); ?>

Any help would be appreciated.

3
Well did you pass a $comment variable to your blade view?Omar Tarek
@foreach($post->comments as $comment) yes, sorry fot not mentioning thischazecka

3 Answers

1
votes

You need a Polymorphic Relations Relationship.

Table Structure

posts
    id - integer
    title - string
    body - text
    user_id - integer

users
    id - integer
    name - string
    email - string
    password - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

Into commentable_id you save the User id or the Post id.
Into commentable_type you save the User Model or Post Model.
And also you can have another table using comments table.

Models

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * Get all of the owning commentable models.
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

class User extends Model
{
    /**
     * Get all of the users's comments.
     */
    public function comments()
    {
        return $this->morphMany('App\Comment', 'commentable');
    }
}

See the docs to get more details.

In this video you have a good explanation https://youtu.be/lePjXdMC6aM

0
votes

You can simply do this in your view.blade.php:

// Here I am assuming you have a $post variable that contains your blog post.
<ul>
    @foreach ($post->comments as $comment)
      <li>{{ $comment->user->name }}</li>
    @endforeach
</ul>

This will loop over all the comments that you have for your blog post and display the owner's name, hopefully this helps.

0
votes

Try this if you need just the name of the person who created comment.

{{ $post->user->name }}

Or