【问题标题】:Access Arguments Input via Artisan Console Command通过 Artisan 控制台命令访问参数输入
【发布时间】:2019-10-17 22:07:38
【问题描述】:

我想像这样运行我的命令

php artisan update:code --code=123

我想从第一个参数中获取代码 - 我似乎无法在 Laravel 网站上找到方法。

<?php

namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;

class updateCode extends Command
{

    protected $signature = 'update:code {code}';


    protected $description = 'Update Code ... ';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {

        $code         = $this->option('code');
        $this->info($code);
        $user         = User::where('type','Admin')->first();
        $user->code   = bcrypt($code);
        $user->active = 1;
        $user->save();

        $this->info($user);

    }
}

我一直得到

“--code”选项不存在。

我也需要定义我的选择吗?

我怎样才能快速访问第一个参数?

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.7 laravel-artisan


    【解决方案1】:

    {code} 用于arguments。对于options,它是{--code}{--code=}

    // option as switch:
    protected $signature = 'update:code {--code}';
    // option with value:
    protected $signature = 'update:code {--code=}';
    

    【讨论】:

    • 我做了php artisan update:code --code=123 并得到了The "--code" option does not accept a value.
    • 请进一步阅读手册。同时,我用{--code=} 示例更新了答案。
    【解决方案2】:

    随便用

    php artisan update:code 123
    

    【讨论】:

    • 我们如何在代码中访问123? 1 美元?你能展示那部分吗?
    猜你喜欢
    • 2022-06-14
    • 2018-07-01
    • 2020-12-12
    • 2019-03-11
    • 2016-04-10
    • 1970-01-01
    • 2015-04-06
    • 2017-01-14
    • 1970-01-01
    相关资源
    最近更新 更多