0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LaravelAdvent Calendar 2024

Day 11

laravel 自作command(バッチ処理)を追加する

Posted at

概要

laravelにて自作のcommand(バッチ処理)を追加する方法をまとめる。
実は下記の記事で記載済みの内容ではあるが、見苦しい記事なので再度書き直す。

方法

  1. 下記コマンドを実行して自作commandを作成

    php artisan make:command EchoHelloWorldCommand
    
  2. 作成されたapp/Console/Commands/EchoHelloWorldCommand.phpを展開、下記のように記載

    app/Console/Commands/EchoHelloWorldCommand.php
    <?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\Log;
    
    class EchoHelloWorldCommand extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'app:echo-hello-world-command';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Execute the console command.
         */
        public function handle()
        {
            $str = 'Hello World';
            echo $str . PHP_EOL;
            Log::info($str);
            return;
        }
    }
    
  3. 下記を実行して作成したcommandを実行「Hello World」が出力され、laravel.logに「Hello World」が出力されていたら完了

    php artisan app:echo-hello-world-command
    
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?