Php 如何使用orm访问用户模型数据

Php 如何使用orm访问用户模型数据,php,laravel,orm,model,Php,Laravel,Orm,Model,我正在尝试做一个论坛,我已经做了线程CRUD和评论, 我想显示发表评论的用户的姓名,我想我把关系搞砸了 我已经尝试添加正确的雄辩关系,但我似乎不明白它是如何工作的 用户模型 public function threads(){ return $this->hasMany(Thread::class); } 线程模型 public function user(){ return $this->belongsTo(User::class);

我正在尝试做一个论坛,我已经做了线程CRUD和评论, 我想显示发表评论的用户的姓名,我想我把关系搞砸了

我已经尝试添加正确的雄辩关系,但我似乎不明白它是如何工作的

用户模型

public function threads(){
        return $this->hasMany(Thread::class);
    }
线程模型

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

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

public function commentable()
    {
        return $this->morphTo();
    }  
用于渲染的显示视图

 @foreach ($thread->comments as $comment)            
            <div class="media">
                <img src="#" class="mr-3" alt="...">
                <div class="media-body">
                    <h5 class="mt-0">{{ $comment->user->name }}</h5>
                    {{$comment->body}}
                </div>
            </div>
        @endforeach
@foreach($thread->comments as$comment)
{{$comment->user->name}
{{$comment->body}
@endforeach
我希望能够访问线程和注释模式的用户名和其他用户数据

但我有一个错误

正在尝试获取非对象的属性“名称”(视图: C:\xampp\htdocs\myforum\resources\views\thread\single.blade.php)


缺少从
用户
注释
的关系?我如何添加它,就像您对
线程()的关系所做的那样。(一个用户有很多线程,但他也有很多注释,我想)不工作只是尝试了在注释模型
public function user(){return$this->belongsTo(user::class);}
中添加这个,还可以使用
和(['comments.user'])一起在获取查询中添加它。