本文介紹了Laravel Eloquent 如何在操作符之間使用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我試圖在 Eloquent 和 Laravel 中找到一種優(yōu)雅的方式來表達
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 如何在操作符之間使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!