使用Laravel Schedule Cronjob更新数据库 [英] Update Database using Laravel Schedule Cronjob

查看:141
本文介绍了使用Laravel Schedule Cronjob更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行Eloquent模型以将数据插入数据库,我试图使用Laravel Schedule和cronjobs进行测试运行,下面是我的代码

I am trying to run an Eloquent model to insert data to the database, I tried to run a test run using Laravel schedule and cronjobs, below is my code

这是我的App\Console\kernel.php

protected $commands = [
    'App\Console\Commands\test',
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{

    $schedule->command('check:test')->everyMinute();

}

还有我的App\Console\Commands\test

protected $signature = 'check:test';
public function handle()
{

        try {
            $test = new Test([
                "user_id" => 1,
                "data_one"=>321,
            ]);
            $test->save();
            return response()->json('successfully added');

        } catch (exception $e) {
            return response()->json('error', $e);

        }

}

和我的crontab -e代码是

* * * * * php /opt/lampp/htdocs/testProj/artisan schedule:run 1>> 
/opt/lampp/htdocs/testProj/schedule.log 2>&1

当执行我得到的日志时,

when excecuted the log I get is,

正在运行的计划命令:'/usr/bin/php7.2''artisan'check:test>'/dev/null'2>& 1

Running scheduled command: '/usr/bin/php7.2' 'artisan' check:test > '/dev/null' 2>&1

数据库中也没有任何变化. 我在做错什么,将不胜感激

And there is no change in the database either. What am i doing wrong, Any help would be appreciated

推荐答案

尝试像crontab -e

* * * * *  php /opt/lampp/htdocs/testProj/artisan schedule:run >> /dev/null 2>&1

kernel.php文件存储区使用laravel登录到日志文件

kernel.php file store log in log file using laravel

protected $commands = [
     Commands\test::class,
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{

    $schedule->command(Commands\test::class)->everyMinute()->appendOutputTo(storage_path('logs/scheduler.log'));

}

和测试命令应该这样

protected $signature = 'check:test';
public function handle()
{
    try {
        $test = new Test()
        $test->user_id = 1;
        $test->data_one= 321;
        $test->save();
        return $this->info('successfully added');

    } catch (exception $e) {
       return $this->warning('successfully added');
    }
}

这篇关于使用Laravel Schedule Cronjob更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆