Laravel Scheduling call controller

后端 未结 5 1643
北荒
北荒 2021-01-04 05:23

I work with Laravel Task Scheduling, but I have a problem when I call some method from my controller.

protected function schedule(Schedule $schedule)
{
    $         


        
相关标签:
5条回答
  • 2021-01-04 05:35

    OK. I solved this problem. The problem was with Docker Cron container. Cron container was not linked with MySQL container. Thanks for all answers.

    0 讨论(0)
  • 2021-01-04 05:41

    Also now I test with more simple function to insert new currency in database. Here is code:

    public function handle()
        {
            $currency                = new Currency();
            $currency->currency_name = 'EUR';
            $currency->save();
    
            $this->info('New currency is successfully generated!');
        }
    

    This function is from laravel/app/Console/Commands. I call from Kernel > schedule(). Not work.

    When I insert simple log write to handle() function like:

    File::put(base_path() . '/storage/logs/test_logs.txt', 'Test log ' . Carbon::now()->format('Y-m-d H:i:s') . PHP_EOL);
    

    this works. But when I try to create new object from Models and save into db -> this not work.

    0 讨论(0)
  • 2021-01-04 05:48

    Directly put this in schedule function in kernel.php file

    $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

    0 讨论(0)
  • 2021-01-04 05:52

    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 ;)

    0 讨论(0)
  • 2021-01-04 05:53

    For me, the first error looks like you need to run composer update, then composer dump-autoload.

    If it works you will also get the second error, the 2002 error meaning is:

    Can't connect to local MySQL server through socket" (see (Client Error Codes and Messages in MySQL docs).

    You need to set your database configuration in your .env file

    0 讨论(0)
提交回复
热议问题