Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

660
Views
Call to undefined method App\Models\Comment::comments()

I wanted to add comment to every post I make but I keep on getting errors.

Comment Controller:

public function store(Request $request)
{
    $comments = new Comment;
    $comments->body =$request->get('comment_body');
    $comments->user()->associate($request->user());
    $blogs = Comment::find(1);
    $blogs->comments()->save($comments);

    return back();
}

Comment Model:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    use HasFactory;
    protected $guarded =[];

    public function blog()
    {
        return $this->belongsTo(Blog::class);
    }

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

Blog Model:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Blog extends Model
{
    use HasFactory;

    protected $fillable = ['user_id' , 'blog_category_id' , 'title' , 'description'];

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

    public function blogcategory()
    {
        return $this->hasOne(BlogCategory::class)->withDefault(function($user , $post){
            $user->name = "Author";
        });
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are using the wrong model; the Blog model has the comments relationship not the Comment model:

$blog = Blog::find(...);
$blog->comments()->save(...);

Update:

You seem to want to be using a Polymorphic relationship it would seem based on the structure of your comments table since you have the fields commentable_id and commentable_type. If you check the documentation for the Polymorphic One to Many relationship this is the same as the example in the documentation:

Blog model:

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

Comment model:

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

Laravel 8.x Docs - Eloquent - Relationships - Polymorphic Relationships - One to Many

Having said that, your Comment model doesn't look like you wanted to use a polymorphic relationship since you specifically had a blog relationship method. If you do not have more than 1 entity that needs to be related to Comment I would not be using a polymorphic relationship.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!