【问题标题】:Raw sql to query builder for whereBetween用于查询 whereBetween 构建器的原始 sql
【发布时间】:2021-07-20 20:47:22
【问题描述】:

SQL 查询:

SELECT a.id, b.employee_id ,b.name as user_name, a.start_from, a.end_to, a.leaving_reason
FROM employee_leaves as a
JOIN users as b on a.user_id = b.id
WHERE (start_from BETWEEN '2021-04-21' AND '2021-04-30')

这工作正常,数据按预期发送。但是当我转换为 laravel 查询生成器时,这不起作用。如果有人可以帮助我。

查询生成器查询:

$data=DB::table('employee_leaves as a')
                        ->join('users as b', 'a.user_id', '=', 'b.id')
                        ->whereBetween('start_from', array($request->from_date, $request->to_date))
                        ->select('a.*', 'b.name as user_name', 'b.id as user_id', 'b.employee_id')
                        ->get();

【问题讨论】:

  • $request->from_date$request->to_dateY-m-d 格式吗?
  • @Autista_z 是的,格式为Y-m-d
  • 你能更具体一点,is not working 是什么意思吗?它不过滤,它返回错误...您可以尝试使用 toSql() insted of get() 并转储创建的 SQL 查询。我们将看到不同之处。
  • @Autista_z 谢谢问题解决了

标签: mysql laravel laravel-query-builder


【解决方案1】:

改变了这个->whereBetween('start_from', array($request->from_date, $request->to_date))

到这个->whereBetween('start_from', [$request->from_date, $request->to_date])

现在这对我来说很好用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2012-12-08
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多