无法在Laravel Blade上获取可变属性。

huangapple go评论61阅读模式
英文:

can't get morphable property on laravel blade

问题

以下是已翻译的内容:

我有以下的Comment(评论)和Post(帖子)模型:

Comment模型关联:

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

Post模型关联:

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

我试图通过评论获取帖子:

$comments = Comment::blog()->with('commentable')->latest()->paginate(50);

到目前为止没有问题,但当我在@foreach循环中的Blade模板中使用commentable->title进行输出时,
它返回尝试读取null属性"title"的错误。

但是当我使用dd($comment->commentable->title)时,它返回帖子标题:

@foreach($comments as $key => $comment)
    {{ dd($comment->commentable->title) }} // 返回帖子标题
    {{ $comment->commentable->title }} // 返回尝试读取null属性"title"的错误
@endforeach

我尝试在Comment模型中使用方法:

public function postTitle(): ?string
{
    return $this->commentable->title;
}

但再次在输出时出现问题,并在dd()中返回标题。

已解决:
帖子上有一个被软删除的评论。当我尝试输出评论时,它返回错误。
如果你遇到这个问题,请不要忘记检查你的数据库数据:)

英文:

I have a Comment and Post models as below:

Comment model relation:

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

Post model relation:

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

i'm try to get a post by its comment:

$comments = Comment::blog()->with('commentable')->latest()->paginate(50);

So far there is no problem, but when i want to echo commentable->title on blade in @foreach loop,
it returns Attempt to read property "title" on null.
but when i use dd($comment->commentable->title), it returns post title,

   @foreach($comments as $key=> $comment)
       {{ dd($comment->commentable->title) }} // returns post title
       {{ $comment->commentable->title }} // returns Attempt to read property "title" on null
   @endforeach

I tried use method in Comment model:

    public function postTitle(): ?string
    {
        return $this->commentable->title;
    }

but again it return problem on echo and returns title on dd()

SOLVED:
There was a softDeleted comment on post. When I tried to echo comment, it returned error.
if you are facing this problem, don't forget to check your db data :))

答案1

得分: 1

Once you using dd() it work as dump() and die(), the problem is when you trying to dd() checking value in a loop, it'll only dump the first value and break the loop, because of die().

In your case, there probably a $comment doesn't have a related commetable, so when you loop to the $comment, it will get a null from $comment->commetable, and you will get the Exception when you trying to access a property title from null.

For a simple verification, you can try:

 @foreach($comments as $key=> $comment)
       {{ !is_null($comment->commentable) ? $comment->commentable->title : 'I do not have any commentable.' }} // returns Attempt to read property "title" on null
 @endforeach
英文:

Once you using dd() it work as dump() and die(), the problem is when you trying to dd() checking value in a loop, it'll only dump the first value and break the loop, because of die().

In your case, there probably a $comment doesn't have a related commetable, so when you loop to the $comment, it will get a null from $comment->commetable, and you will get the Exception when you trying to access a property title from null.

For a simple verification, you can try:

 @foreach($comments as $key=> $comment)
       {{ !is_null($comment->commentable) ? $comment->commentable->title : 'I do not have any commentable.' }} // returns Attempt to read property "title" on null
 @endforeach

huangapple
  • 本文由 发表于 2023年4月19日 19:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定