2015-11-26 92 views
1

在Laravel 5我能找到有使用has方法至少一個註釋的所有帖子:Laravel 5 - ORM具有()的關係(反 - notHas)

// Retrieve all posts that have at least one comment... 
$posts = App\Post::has('comments')->get(); 

正如世界上沒有其他方法一樣notHas,如何我可以找到所有沒有評論的帖子嗎? (記住,這是一個hasMany關係)


參考:

查詢關係存在

http://laravel.com/docs/5.1/eloquent-relationships#querying-relations

回答

4

沒有一個notHas()方法,只要我知道,但找到一個關係計數小於1的記錄通常滿足這一點要求:

$posts = App\Post::has('comments', '<', 1)->get();