首页 > 解决方案 > 你可以在 Laravel 中操作数据库列日期,然后使用 `whereDate()` 子句,而不使用 DB::raw?

问题描述

问题:

你可以在 Laravel 中操作数据库列日期,然后whereDate()在操作的日期上使用子句吗?(使用查询生成器)

我知道我可以使用DB::rawSQL 函数来做到这一点,但我想知道一种不使用的方法DB::raw,像这样我可以利用Carbon或其他 php 和 Laravel 功能。

代码:

->whereDate('sub_weekdays(end_of_month(add_month(date_column)), 10))', '>', today()

我想在数据库上运行这些碳函数date_column,然后进行 '>', today()检查。

研究:

关于 stackoverflow有很多问题whereDatewhereRaw但我遇到的似乎都没有解决这个问题。Laravel 文档也没有讨论如何做到这一点,或者是否有可能。

标签: phplaravellaravel-query-builder

解决方案


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())

推荐阅读