简体   繁体   English

Laravel多对多/从db中选择与另一个不相关的条目

[英]Laravel Many to Many / Choose the entries from db that are NOT associated with another one

I am trying to fetch items from my laravel many to many relationship. 我正在尝试从幼虫中获取多对多关系。

I have posts and tags. 我有帖子和标签。

So there is the possibility that there are tags that are not yet connected to a post. 因此,可能存在尚未连接到帖子的标签。 How can I get those from the DB? 我如何从数据库中获得这些?

$tags = Tag::with('posts')->where('posts', [])->get();
$tags = Tag::with('posts')->where('posts', '')->get();
$tags = Tag::with('posts')->wherePosts('')->get();
$tags = Tag::with('posts')->wherePosts([])->get();

Those do not work. 那些不起作用。 Is there any other way? 还有其他办法吗? If I return 如果我回来

$tags = Tag::with('posts')->get();

I can see those where posts = [] but I dont now how to fetch them. 我可以看到那些帖子= [],但是现在我不知道如何获取它们。

Thanks. 谢谢。

Use querying relations: 使用查询关系:

$tags = Tag::has('posts', '=', 0)->get();

http://laravel.com/docs/eloquent#many-to-many http://laravel.com/docs/eloquent#many-to-many

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM