Search code examples
phplaravellaravel-5laravel-5.7laravel-artisan

Access Arguments Input via Artisan Console Command


I want to run my command like this

php artisan update:code --code=123

I want to get the code from first argument - I can't seems to find a way to do it on Laravel site.

<?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);

    }
}

I kept getting

The "--code" option does not exist.

Do I need to defind my option too ?

How can I just quickly access the first argument ?


Solution

  • {code} is for arguments. For options it is {--code} or {--code=}:

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