laravel 雄辩的 Where 子句中的类似通配符的语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43492310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wildcard-like syntax in an eloquent Where clause?
提问by Robert Brax
Here is a where clause I have:
这是我有的 where 子句:
->where( 'post_type', '=', 'blog' )
Is there any way to replace 'blog' by a wildcard, so it will match any 'post_type' ?
有没有办法用通配符替换 'blog' ,这样它就会匹配任何 'post_type' ?
Full query:
完整查询:
$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;
采纳答案by Pankit Gami
I assume that you are storing blogin any variable, say $blog
.
我假设您将博客存储在任何变量中,例如$blog
.
if($blog) {
$query->where('post_type', '=', $blog);
}
If blog is empty, do not add the where condition.
如果博客为空,则不添加 where 条件。
Edit :
编辑 :
I assume you are using Laravel query builder.
我假设您正在使用 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;
回答by Harsha W
Try this
尝试这个
$query->where($field, 'like', $input . '%');