2017-04-17 5 views
-2

私のデータベースには2つのテーブルがあります。投稿を作成したすべてのユーザーのリストを取得する方法

Users 
     id,name 
    Posts 
     id,user_id,name 

    // user_id is the id who has created the post. 

    How do i get list of all users that have created the posts. 

    Relation in users table is like this. 
    => user HasMany posts. 

    The relation in Post Class 
    => post belongsTo users. 


    What i have tried so far 

    $uses = User::with('posts')->get(); 
    This returns list of all users,that has not even created the post 

このために新しい関係を更新する必要がありますか?

+0

なぜ投票したのですか?情報が不明ですか?質問が理にかなっていない。 –

答えて

1

あなたはこれを試すことができますちなみにこのURL

にLaravel Eloquent.Haveに外観を掘るする必要があります。

$users = User::has('posts')->get(); 

投稿を含むすべてのユーザーのリストを返します。

+0

これは私が遠くに見ていたものです、ありがとう –

関連する問題