问题
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
This is my 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();
}
And my 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);
}
}
and my crontab -e
code is,
* * * * * php /opt/lampp/htdocs/testProj/artisan schedule:run 1>>
/opt/lampp/htdocs/testProj/schedule.log 2>&1
when excecuted the log I get is,
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
回答1:
try to used like that in crontab -e
* * * * * php /opt/lampp/htdocs/testProj/artisan schedule:run >> /dev/null 2>&1
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'));
}
and test command should like this
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');
}
}
来源:https://stackoverflow.com/questions/52851294/update-database-using-laravel-schedule-cronjob