我的查询是:$posts = Post:: whereIn('ID', ( NewsTag:: orderBy('post_date','desc')->with('publisher','newsTopics') ->paginate(12)->pluck('post_id'))) ->get();Newstag.php 模型是这样的: public function newsTopics() { return $this->hasOne('App\Models\NewsTopics', 'post_id', 'post_id'); } public function publisher() { return $this->hasOne('App\Models\Publisher', 'id', 'publisher_id'); } 当我执行以下操作时,我无法访问 publisher 和 newsTopics:@foreach($posts as $post)//doesn't work this way$post->publisher->name;//nor like this$publisher->name;我怎样才能访问发布者和新闻主题?当我运行这个控制器时,它会查询我想要的内容,但我无权访问它。任何帮助,将不胜感激。谢谢你!
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
为了获得模型关系,您必须先在查询中加载它们:
$posts = Post::with('newsTopics','publisher')->
whereIn('ID', (
NewsTag::
orderBy('post_date','desc')->with('publisher','newsTopics')
->paginate(12)->pluck('post_id')))
->get();
然后你可以像这样访问你的关系:
@foreach($posts as $post)
$publisherName= $post->publisher->name;
- 1 回答
- 0 关注
- 77 浏览
添加回答
举报
0/150
提交
取消