Type a terminal command, that displays every Artisan command!
php artisan list
Which terminal command do you use to interact with Eloquent models in a project?
php artisan tinker
Create an Artisan command named “SendEmails”!
php artisan make:command SendEmails
Where can you change the functionality of an Artisan command, that you created?
In the commands class file under the handle() function.
Assuming, that the user used a command incorrectly, which method do you need to use (assuming that you’ll put it in the correct place in the code) to return the exit code 1 and display an error message for the user?
$this->fail('Insert error message here.');
Which interface do you need to implement into your command’s class to stop more than one instance of the command from running at the same time?
use Illuminate\Contracts\Console\Isolatable;
class Example extends Command implements Isolatable
{ // ...
}An isolated command will still exit with a successful exit status code when being ran, while another instance of it is running, even though the new instance won’t do anything. Run the Artisan command “mail:send 1”, which is an isolated command, and let it display the exit status code 12 if another instance of the command is already running!
php artisan mail:send 1 –isolated=12
Given the command argument “mail:send {user}”, how can you make the user argument optional and how can you give that a default value?
// Optional argument...
'mail:send {user?}'
// Optional argument with default value...
'mail:send {user=example}'Create a boolean option for “mail:send {user}”, which is called “queue” and is true if it’s being passed!
‘mail:send {user} {–queue}’
Make an option for the command “mail:send {user}”, that is called “queue” and will receive values!
‘mail:send {user} {–queue=}’
Create the shortcut “Q” for the command ‘mail:send {user} {–queue}’!
‘mail:send {user} {–Q|queue}’
Modify this command ‘mail:send {user}’, so that it can include any amount of users (including 0)!
‘mail:send {user?*}’
Pass the IDs 1 and 2 to this command ‘mail:send {–id=*}’ using Artisan!
php artisan mail:send –id=1 –id=2
Using PromptsForMissingInput, create a function that returns the text “Which user ID should receive the mail?” if the argument “user” isn’t given!
protected function promptForMissingArgumentsUsing(): array
{
return [
'user' => 'Which user ID should receive the mail?',
];
}How could you define the variable “$userId” using the input for the argument “user”?
$userId = $this->argument('user');
Retrieve all arguments entered into a command and define an array “$arguments” with them!
$arguments = $this->arguments();
Define “$queueName” using what the user input as “queue”!
$queueName = $this->option('queue');
Ask the user for their name during the execution of a command and save it to “$name”!
$name = $this->ask('What is your name?');
Ask the user for their password during the execution of the command and save it to the variable “$password”!
$password = $this->secret('What is your password?');
Make the user choose if they want to continue with the command they just entered!
if ($this->confirm('Do you wish to continue?')) {
// ...
}Make the user choose between left or right Twix, name the variable that stores their chose “$side”!
$side = $this->choice(
'Which Twix side tastes better?',
['Left', 'Right'],
$defaultIndex
);How can you run an Artisan command within your code?
$exitCode = Artisan::call( // ... );
Run the Artisan command ‘mail:send’ in your code giving it the IDs 5 and 13 using the “–id” option!
$exitCode = Artisan::call('mail:send', [
'--id' => [5, 13]
]);What do you need to do to add Artisan commands into your queue in your code?
Artisan::queue( // ... );