I am creatint a daily Login, that's checks if user has login for that day and give user point, user on get point once for that day in my user table I have last_login field , and I am using global middleware for easy check, but I noticed user get point any time they logout and login back
class DailyLogin
{
public function handle(Request $request, Closure $next)
{
$user = Auth::user();
$today = Carbon::today();
$yesterday = $today->subDay();
$user->last_login = $today;
$user->save();
if ($user && $user->last_login >= $yesterday) {
// User has logged in within the last 24 hours, do not give them a point
} else {
// User has not logged in within the last 24 hours, give them a point
$user->points += 1;
$user->save();
}
return $next($request);
}
}
In the beginning of the method you're overwriting the information of the last login.
Carbon has a method to check if a date is today. I would use this and after the check update the user:
class DailyLogin
{
public function handle(Request $request, Closure $next)
{
$user = Auth::user();
if (! $user->last_login->isToday()) {
$user->points += 1;
$user->save();
}
$user->last_login = $today;
$user->save();
return $next($request);
}
}