LoginSignup
2
3

More than 5 years have passed since last update.

Laravel Command バッチ処理

Last updated at Posted at 2017-08-17
#/app/Console/SampleCommand.php
//Command名と引数を定義、グロバール変数であり。
protected $signature = 'Command:sample {name}';
#/app/Console/SampleCommand.php
//引数の設定や、出力の設定
 public function handle()
    {
        $name = $this->argument("name");
        $this->info("Hello $name");
    }

テスト

$ php artisan Command:sample haku
Hello haku
//任意引数の指定
protected $signature = 'sample:sample {name?}';

//デフォルトの引数
protected $signature = 'sample:sample {name=laravel}';

//引数の説明
protected $signature = 'sample:sample {name=laravel : 名前を指定} {age? : 年齢を指定}';

会話系:

public function handle()
{
    $this->info('start');

    $name = $this->ask('名前を入力してください');
    $age = $this->ask('年齢を入力してください');

    $this->info("名前 : $name");
    $this->info("年齢 : $age");
    if ($this->confirm('この内容で実行してよろしいですか?')) {
        $this->info("$name $age years old");
    } else {
        $this->info('cancel');
    }

    $this->info('end');
}
//結果
$ php artisan Command:sample  --dry-run
start

 お名前::
 > はく

 年齢:
 > 25

名前:はく
年齢:25

 この内容で実行してよろしいですか (yes/no) [no]:
 > yes

はく 25 year old
end

色分け、簡単な形

    public function handle()
    {
        $this->info('info');
        $this->line('line');
        $this->comment('comment');
        $this->question('question');
        $this->error('error');

        $this->table(
            ['名前', '年齢'],
            [
                ['Taro', 10],
                ['Laravel', 5],
            ]
        );
    }

image.png

エラーメーセージを指定:

    public function handle()
    {
        $this->info('start');

        if ($this->option('force-error')) {
            $this->error('error!');
            return config('command.exit_code.ERROR');
        }

        $this->info('end');
        return config('command.exit_code.SUCCESS');
    }
#config/command.php
return [
    'exit_code' => [
        'SUCCESS' => 0,
        'ERROR' => 1
    ],
];

valitionm使う時

class SampleValidateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sample:validate {name : 名前を指定} {age? : 年齢を指定}';

... 中略

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('start');

        try {

            $this->validate();

        } catch (ValidationException $e) {
            $this->error('validation error!');
            foreach ($e->validator->getMessageBag()->all() as $error) {
                $this->error($error);
            }
            return config('command.exit_code.ERROR');
        }

        $this->info('end');
        return config('command.exit_code.SUCCESS');
    }

    /**
     * Validation
     */
    private function validate()
    {
        \Validator::validate(
            array_filter($this->arguments()),
            [
                'name' => 'max:10',
                'age' => 'numeric|min:20',
            ],
            [
                'name.max' => '名前は10文字以内で入力してください',
                'age.numeric' => '年齢は数値を入力してください',
                'age.min' => '年齢は20才以上で入力してください'
            ]
        );
    }
}

参考:http://qiita.com/nenokido2000/items/abbf70c87c9ad86a2b89

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3