What is artisan ??
Artisan is commandline interface to create controller ,migration,model and also our own commands that will do specific task.Artisan provides list of commands
type : php artisan listCreate command ,follow steps -
- Create Command -
Run composer and type - " php artisan make:command Say " ,
command will be created in console/commands/Say.php - Edit your command file and add logic that needs to be executed -
In Say.php edit --
Edit name and signature of the console command.edit -
protected $signature = 'Say:hello {user}';
{user} - command arguments
eg- 'php artisan Say:hello Omkar'; ,here omkar is argument - You can add dependencies inside constructor and laravel will take care of rest of it.
protected $user;
public function __construct(User $user) {
parent::__construct();
$this->user = $user;
} - Write command logic i.e the code that you want to execute when command runs in artisan
public function handle()
{
$this->comment('Hello : '.$this->user);
}
-
Edit name and signature of the console command.edit -
- Register command
Register command in Console\kernel.php protected $commands = [ Commands\Say::class ]; -
Run Command -
php artisan Say:hello