【问题标题】:Wildcard-like syntax in an eloquent Where clause?雄辩的 Where 子句中的通配符语法?
【发布时间】:2017-09-15 11:33:09
【问题描述】:

这是我的 where 子句:

->where( 'post_type', '=', 'blog' )

有没有办法用通配符替换“博客”,所以它会匹配任何“post_type”?

完整查询:

$db = ( new DbSql() )->db()->getConnection();
$postsCount = $db->table( 'posts' )
                          ->where( 'status', '=', 'published' )
                          ->where( 'post_type', '=', 'blog' )
                          ->where( 'alerts', '<', Settings::CONTENT_ALERT_NUMBER_ALLOWANCE )
                         ->count() ?? null;

【问题讨论】:

  • 删除语句

标签: laravel laravel-5 eloquent laravel-eloquent


【解决方案1】:

使用like:

->where('post_type', 'like', '%'.$string.'%')

【讨论】:

    【解决方案2】:

    试试这个

    $query->where($field, 'like', $input . '%');
    

    【讨论】:

      【解决方案3】:

      我假设您将 blog 存储在任何变量中,例如 $blog

      if($blog) {
          $query->where('post_type', '=', $blog);
      }
      

      如果博客为空,则不添加where条件。

      编辑:

      我假设你正在使用 Laravel 查询生成器。

      $postsQuery = $db->table( 'posts' )
                                ->where( 'status', '=', 'published' );
      if($blog){
          $postQuery->where( 'post_type', '=', $blog );
      }
      $postCount = $postQuery->where( 'alerts', '<', Settings::CONTENT_ALERT_NUMBER_ALLOWANCE )
                               ->count() ?? null;
      

      【讨论】:

      • 你如何将它插入到一个雄辩的陈述中?请显示完整的查询。
      • 请添加您的查询。
      • 添加到原帖中。
      • 这是什么$db?你在用DB门面吗?
      • 已添加。我在 laravel 之外使用 eloquent。所以它基本上返回一个胶囊实例。
      猜你喜欢
      • 2014-09-20
      • 2017-10-18
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2021-02-13
      • 2018-05-19
      • 2017-08-31
      • 2020-07-22
      相关资源
      最近更新 更多