亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 WhereIn 訪問關系

從 WhereIn 訪問關系

PHP
白衣非少年 2023-04-21 15:12:39
我的查詢是:$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;


查看完整回答
反對 回復 2023-04-21
  • 1 回答
  • 0 關注
  • 102 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號