lavavel 定时任务 (command的第二个参数)

之前好像没有写过,记录一下

$schedule->command()方法

第一个参数不用说,可以传纯字符串或者类::class,不过第二个参数确很少人提到

/**
     * Add a new Artisan command event to the schedule.
     *
     * @param  string  $command
     * @param  array  $parameters
     * @return IlluminateConsoleSchedulingEvent
     */
    public function command($command, array $parameters = [])
    {
        if (class_exists($command)) {
            $command = Container::getInstance()->make($command)->getName();
        }

        return $this->exec(
            Application::formatCommandString($command), $parameters
        );
    }

 如上第二个参数是数组,看了源码是拼接在命令后面,如果你传二维数组,它也是只会解析排列在命令后面,

/**
     * Compile parameters for a command.
     *
     * @param  array  $parameters
     * @return string
     */
    protected function compileParameters(array $parameters)
    {
        return collect($parameters)->map(function ($value, $key) {
            if (is_array($value)) {
                $value = collect($value)->map(function ($value) {
                    return ProcessUtils::escapeArgument($value);
                })->implode(' ');
            } elseif (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
                $value = ProcessUtils::escapeArgument($value);
            }

            return is_numeric($key) ? $value : "{$key}={$value}";
        })->implode(' ');
    }

 具体是这个方法

传入 [‘a’, 'b'] 解析完 'a' 'b'
传入 [[‘a’, 'b'], 'c'] 解析完 'a' 'b' 'c'

  

 也可以在方法里触发定时任务

Artisan::call("order:log");

  

原文地址:https://www.cnblogs.com/cxscode/p/11365690.html