How to Create Artisan Command in Laravel with Arguments Support?

Websolutionstuff | Oct-06-2023 | Categories : Laravel

Laravel is a comprehensive framework that provides an extensive range of artisan commands, enabling the automation of diverse tasks such as creating controllers, seeding the databases, and starting servers. However, when developing custom solutions, you may have unique requirements that needs the creation of custom commands. Laravel doesn’t limit you to its commands, you can easily create your own according to your needs.

Here's a guide outlining the steps for creating a new artisan command:
 

Create Command

You can create your own command using the default Laravel artisan command:

php artisan make:command AppointmentReminder

 

Understanding Command

There are 2 main parts in the console command:

  1. Signature property
  2. Handle() method

You can describe the command name in the signature property ($). For example, you are naming the command “send:reminder”, then you have to run the entire command as mentioned below:

php artisan send:reminder

 

Define Arguments

You can easily define optional and required arguments as following:

protected $signature = 'send:reminder {appointmentID}';

(Note: For optional argument, you have to define ? in last 'mail:send {appointmentID?}')
 

Fetch Arguments

After execution of the command and passing of the argument, you have to fetch arguments. Use this default function for that:

public function handle(): void
 {         
     $appointmentID = $this->argument('appointmeentID');     
 }

 

Execute Commands from Code

After you are done with fetching the arguments, you need to execute commands from the code to check accuracy. Run the following command:

Route::post('/send-reminders', function (string $user) {        
     Artisan::call('send:reminder', [             
          'appointmentID' => 123         
     ]);

     // ...     
});

 

Conclusion

Becoming proficient at creating custom artisan commands in Laravel is like having a helpful hand in your web development efforts. It lets you simplify repetitive tasks, work more efficiently, and maintain code elegance.

Laravel's artisan console lets you create custom commands tailored to your project's unique needs. This not only streamlines your work but also adds valuable functionality to your application.

Remember, the more you practice, the better you'll get. So, don't be afraid to experiment and fine-tune your commands. By unlocking the full potential of artisan commands, you'll not only be coding but also sculpting a more efficient and dynamic development experience. So, go ahead, start creating, commanding, and achieving greatness in your Laravel projects!

 


You might also like:

Recommended Post
Featured Post
How to Create Multi Language Website in Laravel
How to Create Multi Language W...

In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...

Read More

Nov-09-2020

How To Add jQuery Datatable Column Filter Dropdown On Top
How To Add jQuery Datatable Co...

In this article, we will see how to add a jquery datatable column filter dropdown on top. Here, we will learn how t...

Read More

Jan-19-2023

How to Create Apexcharts Pie Chart in Laravel 11
How to Create Apexcharts Pie C...

Hello developers! In this article, we'll see how to create apexcharts pie chart in laravel 11. ApexCharts...

Read More

Apr-19-2024

How to Uninstall Composer on Ubuntu 23.04
How to Uninstall Composer on U...

Hey there! If you've found your way to this guide, chances are you're looking to bid farewell to Composer on you...

Read More

Jan-22-2024