請指引我,取誰在laravel
如何,我可以得到所有用戶在Laravel,誰發佈了至少一個職位。並跳過誰沒有發佈任何帖子。
我正在試着這個。但它正在獲取所有用戶。
$users = User::with('post')->get();
請指引我,取誰在laravel
如何,我可以得到所有用戶在Laravel,誰發佈了至少一個職位。並跳過誰沒有發佈任何帖子。
我正在試着這個。但它正在獲取所有用戶。
$users = User::with('post')->get();
嘗試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();
您可以使用:
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();
謝謝,它已經奏效。 – Harry
感謝,它的工作 – Harry
我建議你看一些L araCasts。它確實有助於雄辯的關係。 :) –
感謝您的建議,我一定會觀看LaraCasts視頻。 – Harry