How to make Custom Artisan Command in Laravel

Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks such as seeding the database, generating code, or running scheduled jobs. Here’s how to create a custom Artisan command in Laravel:

  1. Open a terminal window and navigate to your Laravel project directory.
  2. Run the php artisan make:command command followed by the name of your command. For example, if your command is called “send-emails”, you would run php artisan make:command SendEmails. This will create a new command class in the app/console/Commands directory.
  3. Open the command class that was just created and define the signature and description of your command. The signature is the name of the command as it will be typed in the terminal, and the description is a brief explanation of what the command does. You can define these by setting the $signature and $description properties at the top of your command class.
  4. In the handle method of your command class, define the code that should be executed when the command is run. This is where you can add any logic or code that you want to be executed when the command is called.
  5. To register your command with Artisan, open the app/console/Kernel.php file and add your command class to the $commands array.
  6. Run the php artisan list command to see a list of all available Artisan commands, including your custom command. You can now run your custom command by typing php artisan [command-name] in the terminal.

By following these steps, you can easily create custom Artisan commands in Laravel to automate tasks and streamline your workflow.

Ajith Jojo Joseph

Hire Me for Freelancing $30/Hour ,
Contact via email : [email protected]

Share with your friends:

Comments are closed.

How To Generate A PDF from HTML in Laravel 11

Hello , I was trying to generate a PDF payment receipt for my SAAS application and when i search for […]

June 22, 2024

How to create Laravel Flash Messages

Laravel flash messages are a convenient way to display one-time notifications to the user after a form submission or other […]

December 22, 2022

How to make Custom Artisan Command in Laravel

Custom Artisan commands are a useful feature of Laravel that allow you to define your own command-line commands for tasks […]

December 22, 2022

Tips for Laravel migrations

Laravel migrations are a powerful tool for managing and modifying your database schema in a structured and organized way. Here […]

December 22, 2022

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now