Laravel / Laravel Artisan and CLI
Creating and Using Custom Commands
This tutorial will guide you through the process of creating and using custom Artisan commands in Laravel. You will learn how to define your own commands and use them in your Lara…
Section overview
5 resourcesExplores Laravel's Artisan command-line interface for automation and productivity.
Creating and Using Custom Commands in Laravel
1. Introduction
In this tutorial, we are going to learn how to create and use custom Artisan commands in Laravel. Laravel's Artisan command-line tool provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component. We will learn how to define and use our own custom commands within our Laravel projects.
By the end of this tutorial, you will be able to:
- Create custom Artisan commands
- Understand how to use these commands in your Laravel applications
Before we begin, you should have a basic understanding of Laravel and PHP.
2. Step-by-Step Guide
Artisan commands in Laravel are stored in the app/Console/Commands directory. Laravel's make:command Artisan command allows you to generate a new command class in this directory.
Creating a New Command
Run the following command to create a new Artisan command:
php artisan make:command MyCustomCommand
This will create a new command class in app/Console/Commands/MyCustomCommand.php.
Defining Input Expectations
In the created command file, two properties need to be filled in: $signature and $description. $signature is the command that will be typed into the console, and $description is a brief explanation of what the command does.
protected $signature = 'my:command';
protected $description = 'This is my custom command';
Command Logic
The handle method will contain the logic of the command. This method is called when your command is executed.
public function handle()
{
$this->info('My custom command is working');
}
Registering The Command
After creating and defining your new command, you need to register it with Artisan. This is typically done in the commands method of your App\Console\Kernel class.
protected $commands = [
Commands\MyCustomCommand::class,
];
Running The Command
To run your new custom command, you may use the Artisan::call method:
\Artisan::call('my:command');
Or simply in your terminal:
php artisan my:command
3. Code Examples
Here's how your MyCustomCommand.php should look:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MyCustomCommand extends Command
{
protected $signature = 'my:command';
protected $description = 'This is my custom command';
public function handle()
{
$this->info('My custom command is working');
}
}
And here's the Kernel.php:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
Commands\MyCustomCommand::class,
];
protected function schedule(Schedule $schedule)
{
// Your task schedule
}
}
When you run php artisan my:command, you should see the text "My custom command is working" in your terminal.
4. Summary
In this tutorial, we learned how to create a custom Artisan command in Laravel and use it. This is a powerful feature that will allow you to automate various aspects of your application development and deployment process.
For further learning, you can explore how to schedule artisan commands and how to use different types of input and output methods available.
5. Practice Exercises
- Create a command that lists all the users in your database.
- Create a command that creates a new user in your database.
- Create a command that updates a user's information in your database.
Remember, the best way to learn is by doing. So, play around with different command options and see what you can come up with. Good luck!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article