Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

humble_guy's avatar

How user can comment on a post

I have User model

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

Post model

public function comments(){
		 return $this->morphMany(Comment::class, 'commentable');
}

and last Comment model

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

public function commentable(){
		 return $this->morphTo();
}

When I want to create a comment on a post, I use:

$post->comments()->create([
'user_id'=>Auth::User,'text'=>'This is a comment']); 

Is this the only way to add a comment to a post, or is there another possible and easier way?

I imagined another approach, but I'm not sure if it's possible. I want to grab the user first, then find the post the user wants to comment on, and finally add the comment like this:

$user->post->comments()->create();

Is this achievable? Or is there another easier way to handle this? Any advice would be appreciated!

0 likes
8 replies
LaryAI's avatar
Level 58

To allow users to comment on a post, you have set up the relationships correctly in your models. However, there are a few improvements and clarifications that can be made to ensure everything works smoothly.

Models

  1. User Model:

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

    public function comments() {
        return $this->morphMany(Comment::class, 'commentable');
    }
    
  3. Comment Model:

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

Creating a Comment

To create a comment on a post, you can use the create method on the comments relationship of the Post model. However, you need to ensure that the user_id is correctly set. Here is the corrected and improved way to do it:

use Illuminate\Support\Facades\Auth;

// Assuming $post is an instance of the Post model
$post->comments()->create([
    'user_id' => Auth::id(), // Use Auth::id() to get the authenticated user's ID
    'text' => 'This is a comment'
]);

Alternative Method

If you want to make the process more streamlined, you can create a method in your Post model to handle the creation of comments. This can encapsulate the logic and make your controller code cleaner.

Post Model Method

public function addComment($text) {
    return $this->comments()->create([
        'user_id' => Auth::id(),
        'text' => $text
    ]);
}

Using the Method in Controller

use App\Models\Post;
use Illuminate\Support\Facades\Auth;

$post = Post::find($postId); // Assuming $postId is the ID of the post
$post->addComment('This is a comment');

Summary

  • Ensure you use Auth::id() to get the authenticated user's ID.
  • You can encapsulate the comment creation logic in a method within the Post model for cleaner code.

By following these steps, you can efficiently allow users to comment on posts in your application.

1 like
humble_guy's avatar

@tisuchi Thank you, sir. By the way, was my code correct, or should I adjust something?

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@humble_guy You have used one of the ways, ofcourse there are few other way.

For example:

$user = Auth::user(); // Get the authenticated user
$post = Post::find($postId); // Find the post by ID

$user->comments()->create([
    'commentable_id' => $post->id,
    'commentable_type' => Post::class,
    'text' => 'This is a comment'
]);

Another approach:

$user = Auth::user(); // Get the authenticated user
$post = Post::find($postId); // Find the post by ID

$comment = new Comment([
    'text' => 'This is a comment'
]);

$comment->user()->associate($user);
$comment->commentable()->associate($post);
$comment->save();

Laravel has flexibility, now it's in your court how to utilize it based on your needs.

3 likes

Please or to participate in this conversation.