2017-04-17 78 views
-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

你需要深入Laravel Eloquent.Have看看這個URL

通過,你可以嘗試這種方式。

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

它會返回所有有帖子的用戶列表。

+0

這就是我期待的,謝謝 –