mysql - Laravel 选择 Raw 进入 Moloquent 查询
问题描述
以前,我使用 Mysql 作为数据库。一旦数据不断增长,我就会尝试迁移到 MongoDB。我jenssegers/laravel-mongodb
仅用于分析部分。我发现如果我想按月、日、年分组并计数,很难获取数据。
mysql查询
$query = Model::query()
->selectRaw('api_module, MONTH(created_at) as trans, Count(*) as counter');
->whereBetween('created_at', [$start, $end])
->whereNotNull('api_module')
->groupBy('trans', 'api_module')
->orderBy('api_module')
->get(['api_module', 'tran', 'counter']);
数据
{
"_id" : ObjectId("5e61f67d31b9ad636154396f"),
"fingerprint" : "hy9mZ959ovg42898pncA1VPhbqr6gzdQNKDbSVBix8IajZ9yY",
"device_id" : 16,
"device_uuid" : "aaaab-123123",
"api_url" : "http://example.test:8000/api/news",
"api_id" : null,
"api_method" : "GET",
"ip_address" : "127.0.0.1",
"user_agent" : "PostmanRuntime/7.22.0",
"api_module" : "news",
"api_module_studly" : "News",
"origin" : 1,
"request_header" : "ABC",
"request_body" : "ABCD",
"created_at" : "2020-03-06 15:06:37",
"updated_at" : "2020-03-06 15:06:37"
}
如果我想使用Model::raw()
. 如果您能帮助将其从 mysql 转换为 moloquent,将不胜感激
解决方案
推荐阅读
- git - 提交 HEAD 缺失
- azure-functions - dotnet 版本错误的 Azure 函数部署
- node.js - 如何自定义 nest.js 记录器
- r - 增加轴标题和轴之间的边距
- c# - InfluxDB 2 入门示例不向桶中插入任何数据
- amazon-web-services - 通过 CloudFormation 为 AWS::ApiGateway::Method 创建集成时 ARN 无效
- python - 在 Streamlit 中按下按钮时无法增加值
- python - python中函数的时间复杂度
- django - 在身份验证方法中命名为 url kwargs
- sql - 通过比较日期 SQL 过滤数据