Eloquent:Laravel 中查询字段长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37410022/
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
Eloquent: Query the length of field in Laravel
提问by JLM
I want to do something like that in Laravel (valid sqlite query):
我想在 Laravel 中做类似的事情(有效的 sqlite 查询):
select * from 'tbUsers' where length(name)>50;
I tried
我试过
User::with('Permissons')->where('LENGTH(name)','>','50')->get();
But it seems not to work........
不过好像不行。。。。。。
note: other queries works without problem:
注意:其他查询没有问题:
User::with('Permissons')->where('active','=','1')->get();
回答by vijaykumar
Try this
whereRaw( string $sql, array $bindings = array(), string $boolean = 'and')
尝试这个
whereRaw( string $sql, array $bindings = array(), string $boolean = 'and')
User::with('Permissons')->whereRaw('LENGTH(name) > 50')->get();
回答by Javi Stolz
Use whereRaw
:
使用whereRaw
:
User::with('Permissons')->whereRaw('LENGTH(name) > ?', [50])->get();
回答by Wex
In your test you tried 'LENGTH(name)'
, but this converts to a string which means the select statement becomes more like:
在您的测试中,您尝试了'LENGTH(name)'
,但这会转换为字符串,这意味着 select 语句变得更像:
select * from 'tbUsers' where 'LENGTH(name)' > 50;
Which is bad. In order to stop treating it as a string you need to let where() know what you're entering is raw-sql, to do that you can use DB::raw
:
这是不好的。为了停止将其视为字符串,您需要让 where() 知道您输入的是 raw-sql,为此您可以使用DB::raw
:
User::with('Permissons')->where(DB::raw('LENGTH(name)'),'>','50')->get();