Solution:
Use Laravel’s Commands combined with schedule and you will get what you want.
Here’s a detailed explanation:
Step 1: Create a command called “ChangePendingToSuspended”
Open a console then execute
php artisan make:console ChangePendingToSuspended
Step 2: Open up ChangePendingToSuspended.php
you can find it in your app/Console/Commands/
directory and tamper with it’s parameters like adding a description
protected $description = 'Changes the Requests which has been in pending status for a period of time to suspended status.';
and a signature
protected $signature = 'requests:clear-pending';
Ok, before you ask “What’s a signature?” A signature is a way of executing the command from the console for example now you can manually initiate the ChangePendingToSuspended
command from artisan like
php artisan requests:clear-pending
Step 3: Defining our command
Now you put your code into the handle
method in your case it might be something in the context of:
public function handle(){
\DB::table('requests')
->where('created_at','<',\Carbon\Carbon::now()->addMinutes(-20))
->update(['status'=>'suspended']);
}
Just use whatever method that you prefer to change the status in that command.
Step 4: Adding the command to the Schedule
Open up Kernel.php
found in app\Console\
directory you will be presented with an array called $commands, add our class to it
protected $commands = [
Commands\Inspire::class,
Commands\ChangePendingToSuspended::class,
];
Now go to the schedule method and schedule your newly created command
protected function schedule(Schedule $schedule)
{
...
$schedule->command('requests:change-pending-to-investigate')->everyFiveMinutes();
...
}
Ok what’s happening here is that now, every five minutes, the scheduler will execute our command ChangePendingToSuspended
each five minutes, but there’s still 1 more step, we need to make the schedule tick by adding it’s cron job to our system.
Step 5: add the schedule cron entry to your server
this differs among servers and releases, whether you are using windows, or linux or osx
but here’s the cron entry
for linux:
* * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
for windows (using the task scheduler):
* * * * path/to/php path/to/artisan schedule:run 1>> NUL 2>&1