php - 你可以在 Laravel 中操作数据库列日期,然后使用 `whereDate()` 子句,而不使用 DB::raw?
问题描述
问题:
你可以在 Laravel 中操作数据库列日期,然后whereDate()
在操作的日期上使用子句吗?(使用查询生成器)
我知道我可以使用DB::raw
SQL 函数来做到这一点,但我想知道一种不使用的方法DB::raw
,像这样我可以利用Carbon
或其他 php 和 Laravel 功能。
代码:
->whereDate('sub_weekdays(end_of_month(add_month(date_column)), 10))', '>', today()
我想在数据库上运行这些碳函数date_column
,然后进行 '>', today()
检查。
研究:
关于 stackoverflow有很多问题whereDate
,whereRaw
但我遇到的似乎都没有解决这个问题。Laravel 文档也没有讨论如何做到这一点,或者是否有可能。
解决方案
Instead of doing it totally DB::raw()
style, i would believe the most pragmatic approach, would to use DB::raw()
in the column only. You keep the main QueryBuilder
calls, while achieving what you want. The carbon idea you have is not feasible, as you want the query approach, all modifications has to done in SQL
.
->whereDate(DB::RAW('LAST_DAY(date_column)'), now())
推荐阅读
- android - Android Studio 3.2 金丝雀版本不呈现导航标签
- javascript - 卸载 React 父级而不卸载子级
- reactjs - react-redux-firebase 登录承诺在更新身份验证状态之前解决,导致渲染问题
- c++ - 具有方法重载的接口类?
- javascript - 如何使用 Jest 在函数内测试类实例
- loops - Dr-Racket 中的背景声音 [lang #r5rs]
- c++ - FLAC.framework 错误
- oracle - Oracle Cost-Based Optimizer:当标准在维度表上时如何使用事实表外键列的统计信息
- reactjs - 测试反应重定向
- android - 如何将 arrayList 传递给 Tasks.whenAllSuccess(arrayList)?