我使用 whereDate 來獲取給定日期范圍內的資料
$data = Work::where([
['employeeID', '=', $employee],
])->whereDate('date', '>=', $from)
->whereDate('date', '<=', $to)->get();
return $data;
資料范圍
$from = 16-05-2021
$to = 30-05-2021
但它也會回傳不在日期范圍內的資料
{"0":{"id":421,"employeeID":(classified),"jobID":(classified),"date":"31-05-2021","workTime":4,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"4":{"id":425,"employeeID":(classified),"jobID":(classified),"date":"11-06-2021","workTime":6,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"6":{"id":427,"employeeID":(classified),"jobID":(classified),"date":"14-06-2021","workTime":6,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"7":{"id":428,"employeeID":(classified),"jobID":(classified),"date":"20-05-2021","workTime":10,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"9":{"id":430,"employeeID":(classified),"jobID":(classified),"date":"15-06-2021","workTime":10,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"10":{"id":431,"employeeID":(classified),"jobID":(classified),"date":"25-05-2021","workTime":10,"created_at":"2021-12-01T03:13:04.000000Z","updated_at":"2021-12-01T03:13:04.000000Z"},
"24":{"id":445,"employeeID":(classified),"jobID":(classified),"date":"22-05-2021","workTime":10,"created_at":"2021-12-01T03:13:05.000000Z","updated_at":"2021-12-01T03:13:05.000000Z"}}
當日期范圍為 16-05-2021 和 30-05-2021 時,它還回傳第 6 個月的資料
uj5u.com熱心網友回復:
您應該使用 Carbon 轉換日期,以便您可以使用whereDate子句:
->whereDate('date', '>=', \Carbon\Carbon::parse($from)->format('d-m-Y'))
uj5u.com熱心網友回復:
你可以用 whereBetween()
Work::where([
['employeeID', '=', $employee],
])->whereBetween('date', [$from, $to])
->get();
不要忘記正確格式化日期。
use Carbon\Carbon;
$from = Carbon::parse($from)->format('d-m-Y');
// and $to
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/374933.html
