如何为这个普通的 SQL 查询编写 Laravel eloquent 查询(内部查询)

Joy*_*yal -1 laravel eloquent

SELECT *
FROM (
        SELECT *
        FROM appointments
        WHERE `date` <= Curdate()
) `t`
WHERE `t`.`end_time` <= current_time
Run Code Online (Sandbox Code Playgroud)

如何为此 SQL 查询编写 Laravel 等效查询?

Mtx*_*txz 5

如果要查询:

  • 所有过去的约会(从开始到昨天,基于date领域)+
  • 今天end_time没有过去(或正好是现在)的所有今天约会

假设您有一个合适的appointment表模型,您可以尝试以下操作:

Appointments::where('appointments.date', '<', Carbon::now()->toDateString())
    ->orWhere(function ($query) {
        $query->where('appointments.date', Carbon::now()->toDateString())
              ->where('appointments.end_time', '<=', Carbon::now()->toTimeString());
    });
Run Code Online (Sandbox Code Playgroud)

您可能需要测试where子句中使用的 Carbon 格式以确保它与您的数据库存储格式匹配,并且查询返回预期结果。