How to Use Arguments in Laravel Commands
Thank you for your continued support.
This article contains advertisements that help fund our operations.
⇨ Click here for the table of contents for Laravel articles
In Laravel, you can create your artisan commands. This article summarizes how to create those commands and use arguments with them.
Environment
Laravel Framework 8.60.0
What will it look like in the end?
By adding arguments to a command, you can use that argument within the function of the command.
php artisan asitakara-ganbaru 明後日は休む
"明後日は休む"
Creating a Command
Firstly, by "Creating a command",
php artisan your-choice-of-name
you will be able to run the command.
It may seem intimidating at first, but it is remarkably easy to create.
Creating a file with a command
php artisan make:command TestCommand
We have created a file named TestCommand.
Check if the file is created in App\Console\Commands\TestCommand.php.
Deciding on the command name (decide on the ?? in php artisan ??)
Open TestCommand.php and look for the following line:
protected $signature = 'command:name';
You can change this line. You can write any string of your choice, so give it a clear and understandable name.
protected $signature = 'asitakara-ganbaru';
Next, write the function to be executed by this command
Look for the following line in the file:
public function handle()
{
return 0;
}
Replace this code with the following:
public function handle()
{
dd('明日からがんばる');
}
Trying out the command
Let's run the command.
php artisan asitakara-ganbaru
"明日からがんばる"
If you see this output, it means the command was successful.
The command has been executed.
Setting Arguments
Next, let's try setting up the arguments.
protected $signature = 'asitakara-ganbaru {text}';
public function handle()
{
$text = $this->argument('text');
dd($text);
}
Try changing the code like this.
Set the name you want to use as an argument in the signature and call it with $this->argument.
Now, let's run the command with an argument.
php artisan asitakara-ganbaru 明後日は休む
"明後日は休む"
It worked!
Setting up as optional
With the current setup, if you don't provide an argument, the command will throw an error like this:
Not enough arguments (missing: "text").
To allow the command to run even without an argument, add the optional parameter.
protected $signature = 'asitakara-ganbaru {text?}';
Simply add a "?" at the end.
Now, when you execute it without an argument:
php artisan asitakara-ganbaru
null
You have successfully set it as optional.
Setting a default value
You can also set a default value when no argument is provided.
protected $signature = 'asitakara-ganbaru {text=明日から頑張る}';
Let's run it.
php artisan asitakara-ganbaru
"明日から頑張る"
Using an array as an argument
protected $signature = 'asitakara-ganbaru {text*}';
php artisan asitakara-ganbaru 明日から頑張る 明後日も頑張る
array:2 [
0 => "明日から頑張る"
1 => "明後日も頑張る"
]
Optional array
protected $signature = 'asitakara-ganbaru {text?*}';
Related Articles
How to Set Up Periodic Execution (Scheduler) in Laravel
When to Use Laravel's make:command
Summary
That's all for now.
This article is based on the content written in the Laravel documentation with practical examples.
It may be confusing at first to understand where everything is written, but let's gradually get used to it.
For feedback or complaints, please contact me via Twitter DM.
That's all for now!
Popular Articles
Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2
Implementing Breadcrumbs in Laravel with laravel-breadcrumbs