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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:02:27  来源:igfitidea点击:

Laravel Eloquent how to use between operator

phpsqllaravellaravel-4eloquent

提问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