create command laravel 9 in php

To create a command in Laravel 9, you can use the make:command Artisan command. Here are the steps:

  1. Open your terminal and navigate to your Laravel application directory.
  2. Run the make:command command with a name argument to create a new command class. The name argument should follow the App\Commands\ namespace format, e.g. SendEmails.
main.php
$ php artisan make:command SendEmails
38 chars
2 lines
  1. This will generate a new SendEmails class inside the app/Console/Commands directory. You can find it at app/Console/Commands/SendEmails.php.
  2. The generated command class will contain a handle method, which is where you can define the logic for your command. For example, you can add the following code to send an email:
main.php
public function handle()
{
    Mail::to('user@example.com')->send(new MyEmail());
    $this->info('Email sent successfully.');
}
129 chars
6 lines
  1. You can now run your command using the php artisan command. For example:
main.php
$ php artisan send:emails
26 chars
2 lines

This will execute the handle method of your SendEmails command class.

Note: Don't forget to register your command in the app/Console/Kernel.php file. You can do this by adding your command class to the $commands property of the Kernel class, like this:

main.php
protected $commands = [
    \App\Console\Commands\SendEmails::class,
];
72 chars
4 lines

gistlibby LogSnag