What are artisan commands?
What are artisan commands?
Artisan commands are a set of command-line interface (CLI) tools provided by the Laravel framework to assist developers in building and managing their applications. These commands are powered by the Symfony Console component and are designed to automate and streamline various development tasks.
Listing Available Commands:
list command:
php artisan list
help command:
php artisan help migrate
Creating Custom Commands:
make:command or make:console command:
php artisan make:command SendEmails
app/Console/Commands directory, but they can be stored elsewhere as long as they are autoloaded by Composer.Command Structure:
signature and description and a handle method where the command logic is placed.namespace App\Console\Commands;
use Illuminate\Console\Command;
class SendEmails extends Command
{
protected $signature = 'email:send {user} {--queue=default}';
protected $description = 'Send emails to users';
public function handle()
{
// Command logic here
}
}
Registering Commands:
app/Console/Kernel.php file:
protected $commands = [
Commands\SendEmails::class,
];
Executing Commands Programmatically:
Artisan facade:
use Illuminate\Support\Facades\Artisan;
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1,
'--queue' => 'default'
]);
});
Queueing Commands:
queue method:
junior