Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Schedule task isn't working properly

Hey!

I have a schedule task that is running this code every minute (just for test purposes, it is actually running it daily)

$users = DB::table('users')
                    ->where('is_company', 1)
                    ->where('email_verified_at', null)
                    ->whereDate('created_at', '<', Carbon\Carbon::now()->subDays(1))
                    ->get();
            if($users) {
                foreach($users as $user) {
                    $company = DB::table('companies')->where('user_id', $user->id)->first();
                    DB::table('websites')->where('id', $company->website_id)->update(['claimed' => 0]);
                    DB::table('users')->where('id', $user->id)->delete();
                }
            }

But it's not working properly. I double checked if where() & whereDate() criterias of the expected user to be deleted are OK and they are. The user is registered on 2022-04-06 10:53:22 - and the current date is 2022-04-07 around 18:00. The is_company column of the user is set to 1 and its email_verified_at column is also set to null. Is there something wrong with this code? Any idea why it isn't working?

0 likes
18 replies
Sinnbeck's avatar

What isn't working? Error? It's not running at all? It deletes the wrong users?

lat4732's avatar
Level 12

@Sinnbeck It deletes nothing. I see no error in laravel.log. I don't even let it run alone, I run it through php artisan schedule: test to make sure it's really runned.

tykus's avatar

@Laralex it is actually fetching any users?

$users = DB::table('users')
                    ->where('is_company', 1)
                    ->where('email_verified_at', null)
                    ->whereDate('created_at', '<', Carbon\Carbon::now()->subDays(1))
                    ->get();
\Log::debug($users->count());
lat4732's avatar
Level 12

@tykus

[2022-04-07 18:18:19] local.DEBUG: 0  

Data of the expected user

"is_company": 1,
"email_verified_at": null,
"created_at": "2022-04-06 10:53:22",
lat4732's avatar
Level 12

@tykus I have absolutely no idea. I have no idea how the timezones are being determinated at all.

lat4732's avatar
Level 12

I'm finishing work right now. I'll reply back tomorrow.

tykus's avatar

@Laralex so I'll assume you're not working with app timezone other than the default UTC.

Can you turn off some of the constraints in turn to see which (if any) excludes your User?

lat4732's avatar
Level 12

@tykus The email_verified_at check is excluding my expected user.

lat4732's avatar
Level 12

What's wrong with the check of email_verified_at column?

tykus's avatar

@Laralex is it definitely null and not "null"? Can you dump here the result of the query without the email_verified_at constraint?

tykus's avatar

@Laralex and this is all from a Query Builder (rather than Eloquent Builder) query? So there is (i) no global scopes on the query, or (ii) no casts on the result???

Sinnbeck's avatar

Try this

->whereNull('email_verified_at') 
Sinnbeck's avatar

Does it work if you just move it to a route closure and run it directly?

Route::get('delete-users-test', function() {
              $users = DB::table('users')
                    ->where('is_company', 1)
                    ->where('email_verified_at', null)
                    ->whereDate('created_at', '<', Carbon\Carbon::now()->subDays(1))
                    ->get();
          dd($users);
            if($users) {
                foreach($users as $user) {
                    $company = DB::table('companies')->where('user_id', $user->id)->first();
                    DB::table('websites')->where('id', $company->website_id)->update(['claimed' => 0]);
                    DB::table('users')->where('id', $user->id)->delete();
                }
            }
});

Please or to participate in this conversation.