首页 > 解决方案 > 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,将不胜感激

标签: mysqllaravelmongodbmoloquent

解决方案


推荐阅读