[英]how can i solve this raw query
我正在嘗試使用 laravel db 從我的數據庫中獲取一些數據並且它拋出錯誤
$most_purchased_day = (int)Data::select('type')
->whereDate('created_at', '=', date('d'))
->groupBy('type')
->orderByRaw('COUNT(*) DESC')
->first()->network_id;
$most_purchased_day = (int)Data::select('type')
->whereDate('created_at', '=', date('d'))
->groupBy('type')
->orderByRaw('COUNT(*) DESC')
->first()->network_id;
我希望 output 是基於網絡 ID 購買最多的商品
未檢索到第一個用戶,因為created_at
的碳實例不匹配date('d')
更改您的代碼以使用today()幫助程序
$most_purchased_day = (int)Data::select('type')
->whereDate('created_at', '=', today())
->groupBy('type')
->orderByRaw('COUNT(*) DESC')
->first()->network_id;
假設今天創建了 Eloquent model
App\Data {
id: 1,
created_at: "2019-09-22 13:15:19",
updated_at: "2019-09-22 13:15:19",
}
date('d')
只返回一個 integer ,它是當前月份 (22) 的天數,不能匹配2019-09-22 13:15:19
參見PHP:date
使用whereDay
$most_purchased_day = (int)Data::select('type')
->whereDay('created_at', '=', date('d'))
->groupBy('type')
->orderByRaw('COUNT(*) DESC')
->first()->network_id;
whereDate / whereMonth / whereDay / whereYear / whereTime
whereDay
方法可用於將列的值與一個月中的特定日期進行比較:
$users = DB::table('users')
->whereDay('created_at', '31')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.