laravelで簡単なバッチ処理をしてみる


概要

業務でバッチ処理がちゃんと実施できているのか毎日定点観測をしているのだが、そもそもバッチ処理のコードを実際に書いたことがないので簡単なものでいいから実際に自分で作ってみようと思い作ってみることにした。

artisanでcommandクラスを作成する

artisanで下記のコマンドを打つ
コマンド一つで簡単にクラス作れるの便利ですね

$ php artisan make:command Hogecommand

上記コマンド実施後にapp/Console/Commands配下に下記のクラスができていることが確認できると思います。

Hogecommand.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class Hogecommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

  • protected $signature = name
    artisanコマンドで実際に打つ時のコマンド名になる

  • protected $description = バッチ処理の説明

  • public function handle()でバッチ処理の内容を記載する

既存のDBに山田太郎を登録させる

それぞれ、$signature$description、に下記の内容を書き、handle()内で処理を書いてみました。

Hogecommand.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\Test;



class Hogecommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateyamadataro';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'テーブルに山田太郎を登録させる';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $item = new \App\Models\Test;
        $item->text = '山田太郎';
        $item->save();
    }
}

kernel.phpでコマンドを登録する

$commandsの配列内にHogecommandクラスを記載します

Kernel.php
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\Hogecommand::class
    ];

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

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

artisanコマンドでupdateyamadataroを打つ

php artisan updateyamadataro

DBに登録されていることを確認する

artisanコマンドでバッチを叩いて登録させただけの簡単な処理なので、応用的なこともできるように次回もっと工夫したのを作れるようにします

参考サイト