Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

656
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda