【问题标题】:Laravel I cannot pull a single row that is not contained in Query resultLaravel 我无法提取查询结果中不包含的单行
【发布时间】:2019-02-15 13:27:28
【问题描述】:

首先我为我糟糕的英语道歉。

我想先获得category=1 的帖子,然后,只有一个帖子不等于帖子。我写的代码如下,是我的代码有误,还是因为我使用了postgreSql 而发送了不正确的查询。

$posts= Post::where('category',1)->get();

$onlyone = Post::where('id','!=', $posts)->first();

【问题讨论】:

  • 其实你的要求并不清楚。很明显,首先你想获得类别等于 1 的帖子,然后是你想要做的事情。

标签: laravel postgresql orm


【解决方案1】:

这应该可以得到你想要的......

$posts = Post::where('category', 1)->get();
$onlyone= Post::where('category', '!=', 1)->first();

如果您确实有一系列要排除的帖子,您可以执行Post::whereNotIn('id', $posts->pluck('id'))->first(); 之类的操作

【讨论】:

  • 非常感谢,工作就像一个魅力。(“pluck”是我需要的)
猜你喜欢
  • 1970-01-01
  • 2015-02-27
  • 2016-03-19
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
相关资源
最近更新 更多