Laravel Eloquent how to use between operator(Laravel Eloquent 如何在操作符之间使用)
问题描述
我试图在 Eloquent 和 Laravel 中找到一种优雅的方式来表达
I am trying to find an elegant way in Eloquent and Laravel to say
select * from UserTable where Age between X and Y
Eloquent 中是否有 between 运算符(我找不到).
Is there a between operator in Eloquent (I can't find it).
到目前为止我得到的最接近的是像这样链接我的查询
The closest i have gotten so far is chainging my query like this
$query->where(age, '>=', $ageFrom)
->where(age, '<=', $ageTo);
我也遇到过 whereRaw 似乎有效
I also came across whereRaw that seems to work
$query->whereRaw('age BETWEEN ' . $ageFrom . ' AND ' . $ageTo . '');
是否有处理范围的实际 Eloquent 方式(非原始)?
Is there an actual Eloquent way (not raw) that deals with ranges?
推荐答案
$query->whereBetween('age', [$ageFrom, $ageTo]);
看这里:http://laravel.com/docs/4.2/queries#selects一个>
仍然适用于 Laravel 5:https://laravel.com/docs/5.8/queries#where-clauses
Still holds true for Laravel 5: https://laravel.com/docs/5.8/queries#where-clauses
这篇关于Laravel Eloquent 如何在操作符之间使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel Eloquent 如何在操作符之间使用
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
