乱码时光

个人收藏夹

Laravel 自定义 Artisan 命令

2020-3-26 admin

1、新建 Artisan 命令
$ php artisan make:command CalculateActiveUser --command=time:calculate-active-user

2、编辑 Artisan 命令 app/Console/Commands/CalculateActiveUser.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;

class CalculateActiveUser extends Command
{
    /**
     * The name and signature of the console command.
     * 调用命令
     *
     * @var string
     */
    protected $signature = 'time:calculate-active-user';

    /**
     * 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(User $user)
    {
        $this->info("start~");

        // 业务代码...

        $this->info("end~");
    }
}


3、执行 Artisan 命令
$ php artisan time:calculate-active-user