mysql - 具有 DATE mysql 函数的 Laravel 查询生成器

标签 mysql laravel laravel-query-builder

您好,我在我的 laravel 项目中有一个查询,它应该检索范围之间的日期。我的查询是:

 $dateFrom = date("Y-m-d", strtotime($request->dateFrom));
      $dateTo = date("Y-m-d", strtotime($request->dateTo));
      $posts = DB::table('posts')->join('categories','posts.category_id','=','categories.id')->
                join('users','posts.user_id','=','users.id')
                ->select('posts.id','posts.topic','categories.category_name','posts.created_at','users.name')
                ->whereBetween('posts.created_at',[$dateFrom,$dateTo])
                ->get();

我想在 posts.created_at 列上使用 DATE mysql_function,但我不知道该怎么做。以下查询不起作用:

$dateFrom = date("Y-m-d", strtotime($request->dateFrom));
      $dateTo = date("Y-m-d", strtotime($request->dateTo));
       $posts = DB::table('posts')->join('categories','posts.category_id','=','categories.id')->
                join('users','posts.user_id','=','users.id')
                ->select('posts.id','posts.topic','categories.category_name','posts.created_at','users.name')
                ->whereBetween('DATE(posts.created_at)',[$dateFrom,$dateTo])
                ->get();

我很乐意提供帮助。最好的问候 ;)

最佳答案

更改 ->whereBetween('DATE(posts.created_at)'

->whereBetween(DB::raw('DATE(posts.created_at)'))

如果您想在查询中添加一些原始表达式,您可以使用 DB::row 方法,如 laravel documentation 中所述

关于mysql - 具有 DATE mysql 函数的 Laravel 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810536/

相关文章:

php - Laravel 中某个字段出现次数最多的查询

MYSQL更新随机选择X记录

php - 将 Laravel 应用程序集成到插件面板内的 Wordpress(管理员)

php - 在 Laravel 中使用 DB::raw 对 SQL 进行最小-最大规范化

php - Laravel Query Builder 在按 ID 分组的查询中按子查询减去 COUNT

php - Laravel:将查询生成器转换为 Eloquent:ORM

php - 作为 PHP header 信息的数据库主键

java - 使用 PreparedStatement 在 Java 中插入 blob 数据

mysql - 数据帧的类型为 'nonetype' 。我应该如何更改它以允许合并功能运行?

php - 在我的编辑器中管理上传文件的最佳方式?