Laravel - Artisan Console - Command Structure
After generating your command, you should define appropriate values for the signature
and description
properties of the class. These properties will be used when displaying your command on the list
screen. The signature
property also allows you to define your command's input expectations. The handle
method will be called when your command is executed. You may place your command logic in this method.
Let's take a look at an example command. Note that we are able to request any dependencies we need via the command's handle
method. The Laravel service container will automatically inject all dependencies that are type-hinted in this method's signature:
send(User::find($this->argument('user')));
}
}
For greater code reuse, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks. In the example above, note that we inject a service class to do the "heavy lifting" of sending the e-mails.