Laravelで作成したアプリケーションで、cronを使ってDBの更新を定期的に行っているのですが、状況により手動で更新できる機能を実行してほしいと依頼を受けました。
更新は、Kernel.phpにこのように記載
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\UpdateCommand::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('command:update')->dailyAt('4:15');
}
Commands/UpdateCommand.phpに
class UpdateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:update';
public function handle()
{
/* 処理内容 */
}
のように作成。
これを、controllerから呼び出す方法です。
public function index(){
Artisan::call('command:update');
}
これでOK.
Artisanはnamespace App\Http\Controllersの中に含まれているようです。