【问题标题】:Laravel Many to Many / Choose the entries from db that are NOT associated with another oneLaravel 多对多/从 db 中选择与另一个不相关的条目
【发布时间】:2014-02-12 04:20:42
【问题描述】:

我正在尝试从我的 laravel 多对多关系中获取项目。

我有帖子和标签。

因此,有可能存在尚未连接到帖子的标签。我怎样才能从数据库中获取这些?

$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();

那些不起作用。还有其他方法吗?如果我回来

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

我可以看到那些帖子 = [] 但我现在不知道如何获取它们。

谢谢。

【问题讨论】:

    标签: php mysql laravel laravel-4 eloquent


    【解决方案1】:

    使用查询关系:

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

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

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2020-08-27
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多