Laravel Eloquent 如何在操作符之间使用 [英] Laravel Eloquent how to use between operator

查看:25
本文介绍了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 如何在操作符之间使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆