You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: artisan.md
+27-43
Original file line number
Diff line number
Diff line change
@@ -147,11 +147,8 @@ Let's take a look at an example command. Note that we are able to request any de
147
147
148
148
/**
149
149
* Execute the console command.
150
-
*
151
-
* @param \App\Support\DripEmailer $drip
152
-
* @return mixed
153
150
*/
154
-
public function handle(DripEmailer $drip)
151
+
public function handle(DripEmailer $drip): void
155
152
{
156
153
$drip->send(User::find($this->argument('user')));
157
154
}
@@ -167,17 +164,15 @@ Closure based commands provide an alternative to defining console commands as cl
167
164
168
165
/**
169
166
* Register the closure based commands for the application.
170
-
*
171
-
* @return void
172
167
*/
173
-
protected function commands()
168
+
protected function commands(): void
174
169
{
175
170
require base_path('routes/console.php');
176
171
}
177
172
178
173
Even though this file does not define HTTP routes, it defines console based entry points (routes) into your application. Within this file, you may define all of your closure based console commands using the `Artisan::command` method. The `command` method accepts two arguments: the [command signature](#defining-input-expectations) and a closure which receives the command's arguments and options:
179
174
180
-
Artisan::command('mail:send {user}', function ($user) {
175
+
Artisan::command('mail:send {user}', function (string $user) {
181
176
$this->info("Sending email to: {$user}!");
182
177
});
183
178
@@ -191,7 +186,7 @@ In addition to receiving your command's arguments and options, command closures
191
186
use App\Models\User;
192
187
use App\Support\DripEmailer;
193
188
194
-
Artisan::command('mail:send {user}', function (DripEmailer $drip, $user) {
189
+
Artisan::command('mail:send {user}', function (DripEmailer $drip, string $user) {
195
190
$drip->send(User::find($user));
196
191
});
197
192
@@ -200,7 +195,7 @@ In addition to receiving your command's arguments and options, command closures
200
195
201
196
When defining a closure based command, you may use the `purpose` method to add a description to the command. This description will be displayed when you run the `php artisan list` or `php artisan help` commands:
202
197
203
-
Artisan::command('mail:send {user}', function ($user) {
198
+
Artisan::command('mail:send {user}', function (string $user) {
By default, isolation locks expire after the command is finished. Or, if the command is interrupted and unable to finish, the lock will expire after one hour. However, you may adjust the lock expiration time by defining a `isolationLockExpiresAt` method on your command:
243
238
244
239
```php
240
+
use DateTimeInterface;
241
+
use DateInterval;
242
+
245
243
/**
246
244
* Determine when an isolation lock expires for the command.
247
-
*
248
-
* @return \DateTimeInterface|\DateInterval
249
245
*/
250
-
public function isolationLockExpiresAt()
246
+
public function isolationLockExpiresAt(): DateTimeInterface|DateInterval
251
247
{
252
248
return now()->addMinutes(5);
253
249
}
@@ -385,14 +381,10 @@ While your command is executing, you will likely need to access the values for t
385
381
386
382
/**
387
383
* Execute the console command.
388
-
*
389
-
* @return int
390
384
*/
391
-
public function handle()
385
+
public function handle(): void
392
386
{
393
387
$userId = $this->argument('user');
394
-
395
-
//
396
388
}
397
389
398
390
If you need to retrieve all of the arguments as an `array`, call the `arguments` method:
@@ -414,12 +406,12 @@ In addition to displaying output, you may also ask the user to provide input dur
414
406
415
407
/**
416
408
* Execute the console command.
417
-
*
418
-
* @return mixed
419
409
*/
420
-
public function handle()
410
+
public function handle(): void
421
411
{
422
412
$name = $this->ask('What is your name?');
413
+
414
+
// ...
423
415
}
424
416
425
417
The `secret` method is similar to `ask`, but the user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as passwords:
@@ -432,13 +424,13 @@ The `secret` method is similar to `ask`, but the user's input will not be visibl
432
424
If you need to ask the user for a simple "yes or no" confirmation, you may use the `confirm` method. By default, this method will return `false`. However, if the user enters `y` or `yes` in response to the prompt, the method will return `true`.
433
425
434
426
if ($this->confirm('Do you wish to continue?')) {
435
-
//
427
+
// ...
436
428
}
437
429
438
430
If necessary, you may specify that the confirmation prompt should return `true` by default by passing `true` as the second argument to the `confirm` method:
439
431
440
432
if ($this->confirm('Do you wish to continue?', true)) {
441
-
//
433
+
// ...
442
434
}
443
435
444
436
<aname="auto-completion"></a>
@@ -450,7 +442,7 @@ The `anticipate` method can be used to provide auto-completion for possible choi
450
442
451
443
Alternatively, you may pass a closure as the second argument to the `anticipate` method. The closure will be called each time the user types an input character. The closure should accept a string parameter containing the user's input so far, and return an array of options for auto-completion:
452
444
453
-
$name = $this->anticipate('What is your address?', function ($input) {
445
+
$name = $this->anticipate('What is your address?', function (string $input) {
454
446
// Return auto-completion options...
455
447
});
456
448
@@ -482,10 +474,8 @@ To send output to the console, you may use the `line`, `info`, `comment`, `quest
482
474
483
475
/**
484
476
* Execute the console command.
485
-
*
486
-
* @return mixed
487
477
*/
488
-
public function handle()
478
+
public function handle(): void
489
479
{
490
480
// ...
491
481
@@ -528,7 +518,7 @@ For long running tasks, it can be helpful to show a progress bar that informs us
528
518
529
519
use App\Models\User;
530
520
531
-
$users = $this->withProgressBar(User::all(), function ($user) {
521
+
$users = $this->withProgressBar(User::all(), function (User $user) {
532
522
$this->performTask($user);
533
523
});
534
524
@@ -558,10 +548,8 @@ All of your console commands are registered within your application's `App\Conso
@@ -582,12 +570,12 @@ Sometimes you may wish to execute an Artisan command outside of the CLI. For exa
582
570
583
571
use Illuminate\Support\Facades\Artisan;
584
572
585
-
Route::post('/user/{user}/mail', function ($user) {
573
+
Route::post('/user/{user}/mail', function (string $user) {
586
574
$exitCode = Artisan::call('mail:send', [
587
575
'user' => $user, '--queue' => 'default'
588
576
]);
589
577
590
-
//
578
+
// ...
591
579
});
592
580
593
581
Alternatively, you may pass the entire Artisan command to the `call` method as a string:
@@ -623,12 +611,12 @@ Using the `queue` method on the `Artisan` facade, you may even queue Artisan com
623
611
624
612
use Illuminate\Support\Facades\Artisan;
625
613
626
-
Route::post('/user/{user}/mail', function ($user) {
614
+
Route::post('/user/{user}/mail', function (string $user) {
627
615
Artisan::queue('mail:send', [
628
616
'user' => $user, '--queue' => 'default'
629
617
]);
630
618
631
-
//
619
+
// ...
632
620
});
633
621
634
622
Using the `onConnection` and `onQueue` methods, you may specify the connection or queue the Artisan command should be dispatched to:
@@ -644,16 +632,14 @@ Sometimes you may wish to call other commands from an existing Artisan command.
644
632
645
633
/**
646
634
* Execute the console command.
647
-
*
648
-
* @return mixed
649
635
*/
650
-
public function handle()
636
+
public function handle(): void
651
637
{
652
638
$this->call('mail:send', [
653
639
'user' => 1, '--queue' => 'default'
654
640
]);
655
641
656
-
//
642
+
// ...
657
643
}
658
644
659
645
If you would like to call another console command and suppress all of its output, you may use the `callSilently` method. The `callSilently` method has the same signature as the `call` method:
@@ -669,10 +655,8 @@ As you may know, operating systems allow signals to be sent to running processes
0 commit comments