2017-04-17 2 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

이것은 내가 지금까지보고 있었던 것입니다. 감사합니다. –

관련 문제