2017-05-09 16 views
0
至少一種後所有用戶

請指引我,取誰在laravel

如何,我可以得到所有用戶在Laravel,誰發佈了至少一個職位。並跳過誰沒有發佈任何帖子。

我正在試着這個。但它正在獲取所有用戶。

$users = User::with('post')->get(); 

回答

0

嘗試has()方法雄辯車型 - >使用whereHas()活躍的帖子https://laravel.com/docs/5.4/eloquent-relationships

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

你可以把用戶。記住它。 :)

$users = User::with('post')->whereHas('post', function ($query) { 
    $query->where('is_active', '=', true); 
})->get(); 
+0

感謝,它的工作 – Harry

+0

我建議你看一些L araCasts。它確實有助於雄辯的關係。 :) –

+0

感謝您的建議,我一定會觀看LaraCasts視頻。 – Harry

0

試試這個:

$users = User::has('post')->get(); 
+0

謝謝,尋求幫助 – Harry

0

您可以使用:

User::with('post')->has('post')->get(); 

whereHas(從laravel doc

// Retrieve all posts with at least one comment containing words like foo% 
$posts = Post::whereHas('comments', function ($query) { 
    $query->where('content', 'like', 'foo%'); 
})->get(); 
+0

謝謝,它已經奏效。 – Harry