0

I'm using laravel Scheduler, i want to set a subscription status to expired if a certain condition is met, and also need this to run every mins in the background..Figured task scheduler is the best option but couldn't figure out why the code is'nt executing after running php artisan schedule:work ..Below is my code Located in App/Console/Kernel.php (schedule function)

  $schedule->call(function () {
        $checkSubStatus = UserSubscription::where('status', 'approved')->where('users_id', auth()->user()->id)->first();
        $currentTime = Carbon::now();
        if($currentTime > $checkSubStatus->expiry_date) {
            $checkSubStatus->update([
                'status' => 'expired'
            ]);
        }
    })->everyMinute();

But works when i just delete the table, like so DB::table('users_subscription')->delete(); Please any help?

6
  • Is the task scheduler not working or the code inside the closure (function)? I mean, are you sure this code is executed? Commented Aug 21, 2021 at 14:12
  • The task scheduler is working once i run php artisan schedule:work but the code does'nt get executed, unless i try something else like delete a table Commented Aug 21, 2021 at 14:18
  • You're trying to get user's id with session information, but it won't work in queued or scheduled jobs because session does not exist in that time. because of this probably you're getting error in this block "auth()->user()->id" Commented Aug 21, 2021 at 14:30
  • Ohh myy..is there any alternative?? Commented Aug 21, 2021 at 14:37
  • You are super right , it works when i hard code an id in there, Is there a way to combat this? Commented Aug 21, 2021 at 14:38

1 Answer 1

1

You have an exception caused by auth()->user()->id, since no user is logged when your closure is executed.

You can check the logs in storage/logs/laravel.log and if it is the case, the solution is just to avoid using any authentification mechanisms inside the scheduler.

As an alternative, you have to rethink what UserSubscription should be expired:

UserSubscription::where('status', 'approved')
->where('expiry_date', '<', now())
->update(['status' => 'updated');

No user anymore, you just expire every subscriptions where the date is before now.

Credit to Emre Kaya for finding the error.

Sign up to request clarification or add additional context in comments.

5 Comments

In deed, i updated my answer. Emre Kaya, if you write an answer i'll be glad to delete mine.
You're right tho,i can see the error logs...Somethin about call to update function on null which indicates that the id is'nt working. is there any solution for this ?
I updated my answer, maybe it can help you, if you have any question tell me
Damnn..Best answer ever, Thanks so much, it works perfectly..how come i didnt think like this..lol.Most grateful
No problem :) I see you are new on SO, don't forget to validate the answer (here and in your other questions) so other people with the same kind of error can quickly understand this is what they are looking for. Have a nice day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.