有沒有辦法跳過某個月份?我只需要顯示一月、二月、九月、十月、十一月和十二月。
這是我的代碼:
$emptyMonth = ['count' => 0, 'month' => 0];
for ($i = 1; $i <= 12; $i ) {
$emptyMonth['month'] = $i;
$monthlyArray[$i - 1] = $emptyMonth;
}
$data = DB::table('doc')
->select(DB::raw('count(*) as count,MONTH(created_at) as month'))
->where('status', 'done')
->where('created_at', '>=', Carbon::parse('first day of january'))
->where('created_at', '<=', Carbon::parse('last day of december'))
->whereyear('created_at', Carbon::now())
->groupBy('month')
->orderBy('month')
->get()
->toarray();
foreach ($data as $key => $array) {
$monthlyArray[$array->month - 1] = $array;
}
$result = collect($monthlyArray)->pluck('count');
有沒有辦法跳過或不顯示本月的某些特定內容?
uj5u.com熱心網友回復:
你可以使用whereMonth:
$data = DB::table('doc')
->select(DB::raw('count(*) as count,MONTH(created_at) as month'))
->where('status', 'done')
->where('created_at', '>=', Carbon::parse('first day of january'))
->where('created_at', '<=', Carbon::parse('last day of december'))
->whereyear('created_at', Carbon::now())
->where(function($query){
$query->whereMonth('created_at',1)
->orWhereMonth('created_at',2)
->orWhereMonth('created_at',9); // extra
})
->groupBy('month')
->orderBy('month')
->get()
->toarray();
或者干脆使用 raw 和Month mysql 函式來做:
$query->whereIn(DB::raw('MONTH(created_at)'),[1,2,3,9]);
uj5u.com熱心網友回復:
您可以在 foreach 中制作此過濾器。假設您將月份作為資料庫中的數字:
foreach ($data as $key => $array) {
if(in_array($array->month, [1,2,9,10,11,12]))
{
$monthlyArray[$array->month - 1] = $array;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364839.html
上一篇:會話放置似乎無法正常作業
