BindingResolutionException when using constructor parameters in a Laravel 5 command

I make a command that is called through the controller. When I make a simple example of a command and controller like this, it works:

//Controller
$command = new TestCommand();
$this->dispatch($command);

//Command
public $name;

public function __construct()
{
        $this->name = 'hi';
}

public function handle(TestCommand $command)
{
        dd($command->name);
}

When I call the command through the controller, I get a "hello", which is correct. But when I try to pass something through the constructor, I get an exception to resolve the binding:

//Controller
$command = new TestCommand('hi');
$this->dispatch($command);

//Command
public $name;

public function __construct($name)
{
        $this->name = $name;
}

public function handle(TestCommand $command)
{
        dd($command->name);
}

Why is this? What I did is similar to what I found in the Laravel docs example, but I get this exception:

BindingResolutionException in line Container.php 872: Resolution of an unsolvable dependency [Parameter # 0 [$ name]] in the class App \ Commands \ TestCommand

+4
source share
1 answer

- . , User $user Guard $auth, Laravel , . Laravel , .

docs:

, , handle .

-

+2

Source: https://habr.com/ru/post/1589494/


All Articles