laravel對get查詢結果集可以foreach迴圈where查詢
例如:
use App\Models\User\User;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
$start_at=’2021-11-30’;
$end_at=’2021-12-01’;
$days = new CarbonPeriod($start_at, ‘1 days’, $end_at);
$user = User::selectRaw(‘date_format(created_at, “%Y-%m-%d”) as date, count(id) as count)
->whereBetween(‘created_at’, [$start_at, date(‘Y-m-d’, strtotime(“$end_at +1 days”))])
->groupBy(‘date’)
->orderBy(‘date’,’DESC’)
->get();
$result = [];
foreach ($days as $day) {
$user_count = $user->where(‘date’, $day->toDateString())->first();
$result[] = [
‘date’ => $day->toDateString(),
‘count’ => $user_count->count ?? 0
];
}
return $result;
本作品採用《CC 協議》,轉載必須註明作者和本文連結