I work with Laravel Task Scheduling, but I have a problem when I call some method from my controller.
protected function schedule(Schedule $schedule)
{
$schedule->call('UserController@deleteInactiveUsers')->everyMinute();
//$schedule->call('App\Http\Controllers\UserController@deleteInactiveUsers')->everyMinute();
}
When I call with uncommented line i get this error:
[ReflectionException]
Class RecurrenceInvoiceController does not exist
and then I insert fully qualified namespace path and then I get this error:
[PDOException] SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
And
[ErrorException] PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Where is the problem? Which way is correct to call method from Controller from Laravel Task Scheduling.
In fact, there are a couple of different ways in which Laravel allows you to define schedule tasks: Use the closure/callable. Call the artisan command. Execute the shell command. Moreover, there are plenty of built-in scheduling frequencies you could choose from:
In Laravel Artisan facade that way we can easily run the all artisan command also with argument. So Artisan facade have two method one call () and another one is queue () through we can simply make process in call like seeder and also migration run etc. So Here in bellow example we can learn how can we run artisan commands from controller.
Yes, you can write a a shell script that will make a CurlRequest to your controller, and add the shell script to a cron job. or you can use laravel commands and call the controller by a request. why not using the code inside the controller in a command?
You can learn more about Cron job on Wikipedia. However, Laravel Cron Job Scheduling makes the whole process very easy. Laravel Cron Job is an inbuilt task manager that gives your applications the ability to execute specific commands like sending a slack notification or removing inactive users at a periodic time.
I stumbled months ago with the same problem, until I could fix it. I use laravel 5.2 and the kernel call my drivers this way:
$schedule->call('App\Http\Controllers\MyController@MyAction')->everyMinute();
I hope this will help someone else ;)
Directly put this in schedule function in kernel.php
.
$schedule->call('App\Http\Controllers\YourController@function')->everyMinute();
This will run after every minute.
Note:
In localhost, you have to run it manually but on the server, you should try this command in your cronjob this will automatically call your controller function after every minute
php -d register_argc_argv=On /home/iflasity/public_html/foldername /artisan schedule:run > /dev/null 2>&1
cd /home/iflasity/public_html/foldername && php artisan schedule:run /dev/null 2>&1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With