简体   繁体   English

为什么在尝试访问“$comment->users->username”时出现“试图获取非对象的属性‘用户名’”?

[英]Why am I getting "Trying to get property 'username' of non-object" when trying to access "$comment->users->username"?

I'm trying to get the usernames of the users who have posted comments, however, I keep getting "Trying to get property 'username' of non-object" error.我正在尝试获取发表评论的用户的用户名,但是,我不断收到“试图获取非对象的属性‘用户名’”错误。 I believe my relationships are done correctly but I might be wrong.我相信我的人际关系是正确的,但我可能是错的。

My tables:我的表:

Table: Users
Columns: id, username, password

Table: Comments
Columns: id, user_id, image_id, comment

Comment model:评论型号:

class Comment extends Model
{
    public function users(){
        return $this->belongsTo('App\User');
    }
}

User model:用户模型:

class User extends Model implements Authenticatable
{
    public function comments(){
        return $this->hasMany('App\Comment');
    }
}

I'm getting the comments like this:我收到这样的评论:

$comments = Comment::with('users')->where('image_id', $id)->get();

And then trying to loop through them in the view like this:然后尝试在视图中循环它们,如下所示:

@foreach($comments as $comment)
    <p>{{ $comment->comment }}</p>
    <p>{{ $comment->users->username }}</p>
@endforeach

And when I dd($comments), I see:当我 dd($comments) 时,我看到:

 #relations: array:1 [▼
        "users" => null
 ]

I'm not sure why is it null though.我不确定为什么它是空的。

Try this尝试这个

@foreach($comments as $comment)
    <p>{{ $comment->comment }}</p>
    <p>{{ !empty($comment->users->username) ? $comment->users->username : '' }}</p>
@endforeach

Also I suggest you change you relations name users to user另外我建议您将关系名称users更改为user

class Comment extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }
}

Get comments获取评论

$comments = Comment::with('user')->where('image_id', $id)->get();

And in view并且鉴于

@foreach($comments as $comment)
    <p>{{ $comment->comment }}</p>
    @if(!empty($comment->user))
        <p>{{ $comment->user->username }}</p>
    @endif
@endforeach

try this way :试试这种方式:

@foreach($comments as $comment)
    <p>{{ $comment->comment }}</p>
    <p>{{ $comment->users["username"] }}</p>
@endforeach
@foreach($comments as $comment)
<p>{{ $comment->comment }}</p>
<p>{{ optional($comment->users)->username}}</p> 
@endforeach

Try this approach.试试这个方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图获取错误关注的非对象的属性<td> &#39;.$users-&gt;用户名。&#39;</td> - Trying to get property of non-object the error focuses on <td>'.$users->username.'</td> 尝试使用MODELS获取用户名,但出现此错误:尝试获取非对象的属性 - Trying to get the username using MODELS but getting this error: Trying to get property of non-object 为什么会出现下一个错误:试图获取非对象的属性? laravel - Why am I getting next error: Trying to get property of non-object? laravel 为什么我要尝试获取AngularJs中非对象错误的属性? - Why i am getting trying to get property of non-object error in AngularJs? 为什么我要在Laravel Eloquent中尝试获取非对象的属性 - Why I am getting Trying to get property of non-object in Laravel Eloquent 为什么当“owner_user”中有信息时,我会尝试获取非对象的属性“用户名”? - Why do I get Trying to get property 'username' of non-object while “owner_user” has information in it? 为什么我在 PHP 中收到“试图获取非对象的属性”错误? - Why am I getting a "trying to get a property of a non-object" error in PHP? 为什么我收到通知:试图获取非对象错误的属性? - Why am I getting Notice: Trying to get property of non-object error? 尝试访问数组时尝试获取非对象的属性 - Trying to get property of non-object when trying to access array 试图获取非对象的属性“用户” - Trying to get property 'users' of non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM