【问题标题】:Laravel - Return All Posts Except some post with ConditionLaravel - 返回所有帖子,除了一些有条件的帖子
【发布时间】:2020-09-21 09:15:24
【问题描述】:

我想获取帖子,但不是具有特定键值的帖子

我使用下面的代码来获取我的帖子

$col = Post::where('type', $type)->whereNotIn('restrict', ['res1'])->orderBy('id', 'desc')->paginate(24);

它可以工作,但它也不会返回具有 null 限制值的帖子

我如何获得帖子除了一些具有限制键值 == 'res1' 的主题?

【问题讨论】:

    标签: jquery laravel collections


    【解决方案1】:

    它不会返回空值,因为您使用了 whereNotIn。这意味着它将仅返回具有除“res1”和空值之外的值的行。因此,每当您应用条件时,mysql 认为该字段不能为空。

    要克服这种情况,请使用以下方法

    $col = Post::where(function ($query) {
            $query->whereNotIn('restrict', ['res1'])->orWhereNull('restrict');                  
        })->orderBy('id', 'desc')->paginate(24);
    

    【讨论】:

      【解决方案2】:

      尝试将where!= 运算符一起使用。

      $col = Post::where('type', $type)->where('restrict', '!=' , 'res1')->orderBy('id', 'desc')->paginate(24);
      

      【讨论】:

        猜你喜欢
        • 2016-11-04
        • 2012-08-14
        • 2022-11-08
        • 1970-01-01
        • 2017-04-03
        • 1970-01-01
        • 2021-07-09
        • 2016-06-14
        • 1970-01-01
        相关资源
        最近更新 更多