I have created the command php artisan make:command expiration
in Laravel 11 and I want to activate the file as it was in previous versions of Laravel.
Initially the command
file was activated by adding it inside the kernel file, but the kernel file is not present in newer versions of Laravel. How do I activate the command
file and specify a period to run it as it was in previous versions of Laravel?
The command
file I created:
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
class expiration extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:expiration';
/**
* The console command description.
*
* @var string
*/
protected $description = 'expir user every 5 minute automatically';
/**
* Execute the console command.
*/
public function handle()
{
$users_expir = User::where('expiration',0)
->update('expiration',1);
}
}
Now how do I activate this "command" file and specify a specific period for it to work after, as was the case in previous Laravel versions?
I may not fully understand your question.. maybe you can link to examples of those previous versions?
If you mean "register" a command file, then according to the documentation:
By default, Laravel automatically registers all commands within the
app/Console/Commands
directory....
If necessary, you may also manually register commands by providing the command's class name to the
withCommands
method (inbootstrap/app.php
):
use App\Domain\Orders\Commands\SendEmails;
->withCommands([
SendEmails::class,
])
When Artisan boots, all the commands in your application will be resolved by the service container and registered with Artisan.
If you want to schedule the command every 5 minutes, then you can use Laravel's task scheduling:
// routes/console.php or bootstrap/app.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('user:expiration')->everyFiveMinutes();