php Laravel Eloquent 如何在运算符之间使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26746918/
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
Laravel Eloquent how to use between operator
提问by GRowing
I am trying to find an elegant way in Eloquent and Laravel to say
我试图在 Eloquent 和 Laravel 中找到一种优雅的方式说
select * from UserTable where Age between X and Y
Is there a between operator in Eloquent (I can't find it).
Eloquent 中是否有 between 运算符(我找不到)。
The closest i have gotten so far is chainging my query like this
到目前为止我得到的最接近的是像这样链接我的查询
$query->where(age, '>=', $ageFrom)
->where(age, '<=', $ageTo);
I also came across whereRaw that seems to work
我也遇到过似乎有效的 whereRaw
$query->whereRaw('age BETWEEN ' . $ageFrom . ' AND ' . $ageTo . '');
Is there an actual Eloquent way (not raw) that deals with ranges?
是否有处理范围的实际 Eloquent 方式(非原始)?
回答by jsphpl
$query->whereBetween('age', [$ageFrom, $ageTo]);
Look here: http://laravel.com/docs/4.2/queries#selects
看这里:http: //laravel.com/docs/4.2/queries#selects
Still holds true for Laravel 5: https://laravel.com/docs/5.8/queries#where-clauses
Laravel 5 仍然适用:https://laravel.com/docs/5.8/queries#where-clauses