2017-04-19 25 views
0

这里是一个where子句我有:雄辩的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; 
+0

只需删除语句 –

回答

0

我假设你存储博客在任何变量,说$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; 
+0

如何将此插入到雄辩声明中?请用这个显示一个完整的查询。 –

+0

请添加您的查询。 –

+0

已添加原始帖子。 –

2

使用like

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

试试这个

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