天天看点

Laravel 5.6 创建命令 command

创建command

php artisan make:command UpdateCameraStatus --command=camera:update_status
           

bogon:it richard$ php artisan make:command UpdateCameraStatus --command=camera:update_status

Console command created successfully.

在app/Console/Commands 下 创建了 UpdateCameraStatus.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * 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()
    {
        //do something
        $count = $this->argument('count');
    }
}
           

定义参数

protected $signature = 'camera:update_status {count}';
           

获取参数

$arguments = $this->arguments();

$count = $this->argument('count');
           

在 handle 编码(要执行的操作)

执行命令:

php artisan camera:update_status 100
           

done