post 模型和 user 模型的视图层的一个语法问题

 @foreach($posts as $post)
                        <div class="blog-post" style="margin-top: 30px">
                            <p class=""><a href="/user/{{$post->user->id}}">{{$post->user->name}}</a>{{$post->created_at->diffForHumans()}}</p>
                            <p class=""><a href="/posts/{{$post->id}}" >{{$post->title}}</a></p>


                            <p><p>{!! str_limit($post->content,100,'...')!!} </p>
                        </div>
                         @endforeach
{{$post->user->id}}

分别有posts模型和User模型,{{$post->user->id}}这样的写法是因为user模型定义了hasmany 和posts模型的关联吗? 为什么用dd()函数打印不出来?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

Post模型中定义一个与User模型的反向关联:

public function user()
{
    return $this->belongsTo(User::class);
}
4年前 评论
讨论数量: 1

Post模型中定义一个与User模型的反向关联:

public function user()
{
    return $this->belongsTo(User::class);
}
4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!