Larave Extracting Two Dates Out of One
Extracting two dates out of one, like in the case of startOfMonth & endOfMonth, make sure not to apply both functions on the same date variable. Otherwise, you get unexpected results. 👇
class GetUserTokesConsuemed
{
public function execute(User $user, Carbon $date = null):mixed
{
$date = $date ?? Carbon::now();
// ❌ don't do
$startDate = $date->startOfMonth();
$endDate = $date->endOfMonth();
// ✔️ do
$startDate = $date->startOfMonth();
$endDate = $date->copy()->endOfMonth(); // make a copy
// ...
}
}

Md Najmul Hasan
https://shouts.dev/imnhasan
Comments
No comments yet…