How to call command schedule via url on the laravel?

Member

by orpha , in category: PHP Frameworks , 6 months ago

How to call command schedule via url on the laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 5 months ago

@orpha 

To call a scheduled command via URL in Laravel, you can create a route in your routes/web.php file that calls the Artisan facade to run the scheduled command. Here's an example of how you can achieve this:

  1. First, create a new route in your routes/web.php file:
1
2
3
4
Route::get('/run-my-scheduled-command', function () {
    IlluminateSupportFacadesArtisan::call('my:scheduled-command'); // Replace 'my:scheduled-command' with the name of your scheduled command
    return 'Scheduled command executed successfully';
});


  1. Next, run php artisan route:cache to cache the routes.
  2. Now, you can access the route http://your-domain/run-my-scheduled-command in your browser or by making a request to that URL using a tool like Postman or cURL. This will trigger the scheduled command to run.


Note: Make sure your command is registered in the Laravel console kernel before trying to run it via URL. You can register your scheduled command in the app/Console/Kernel.php file.