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
-[Calling Commands From Other Commands](#calling-commands-from-other-commands)
21
21
-[Stub Customization](#stub-customization)
22
+
-[Events](#events)
22
23
23
24
<aname="introduction"></a>
24
25
## Introduction
@@ -36,12 +37,16 @@ Every command also includes a "help" screen which displays and describes the com
36
37
37
38
Laravel Tinker is a powerful REPL for the Laravel framework, powered by the [PsySH](https://github.com/bobthecow/psysh) package.
38
39
40
+
<aname="installation"></a>
39
41
#### Installation
40
42
41
43
All Laravel applications include Tinker by default. However, you may install it manually if needed using Composer:
42
44
43
45
composer require laravel/tinker
44
46
47
+
> {tip} Looking for a graphical UI for interacting with your Laravel application? Check out [Tinkerwell](https://tinkerwell.app)!
48
+
49
+
<aname="usage"></a>
45
50
#### Usage
46
51
47
52
Tinker allows you to interact with your entire Laravel application on the command line, including the Eloquent ORM, jobs, events, and more. To enter the Tinker environment, run the `tinker` Artisan command:
@@ -54,6 +59,7 @@ You can publish Tinker's configuration file using the `vendor:publish` command:
54
59
55
60
> {note} The `dispatch` helper function and `dispatch` method on the `Dispatchable` class depends on garbage collection to place the job on the queue. Therefore, when using tinker, you should use `Bus::dispatch` or `Queue::push` to dispatch jobs.
56
61
62
+
<aname="command-whitelist"></a>
57
63
#### Command Whitelist
58
64
59
65
Tinker utilizes a white-list to determine which Artisan commands are allowed to be run within its shell. By default, you may run the `clear-compiled`, `down`, `env`, `inspire`, `migrate`, `optimize`, and `up` commands. If you would like to white-list more commands you may add them to the `commands` array in your `tinker.php` configuration file:
@@ -62,6 +68,7 @@ Tinker utilizes a white-list to determine which Artisan commands are allowed to
62
68
// App\Console\Commands\ExampleCommand::class,
63
69
],
64
70
71
+
<aname="classes-that-should-not-be-aliased"></a>
65
72
#### Classes That Should Not Be Aliased
66
73
67
74
Typically, Tinker automatically aliases classes as you require them in Tinker. However, you may wish to never alias some classes. You may accomplish this by listing the classes in the `dont_alias` array of your `tinker.php` configuration file:
@@ -160,6 +167,7 @@ Even though this file does not define HTTP routes, it defines console based entr
160
167
161
168
The Closure is bound to the underlying command instance, so you have full access to all of the helper methods you would typically be able to access on a full command class.
162
169
170
+
<aname="type-hinting-dependencies"></a>
163
171
#### Type-Hinting Dependencies
164
172
165
173
In addition to receiving your command's arguments and options, command Closures may also type-hint additional dependencies that you would like resolved out of the [service container](/docs/{{version}}/container):
@@ -171,6 +179,7 @@ In addition to receiving your command's arguments and options, command Closures
171
179
$drip->send(User::find($user));
172
180
});
173
181
182
+
<aname="closure-command-descriptions"></a>
174
183
#### Closure Command Descriptions
175
184
176
185
When defining a Closure based command, you may use the `describe` 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:
@@ -331,6 +340,7 @@ The `secret` method is similar to `ask`, but the user's input will not be visibl
331
340
332
341
$password = $this->secret('What is the password?');
333
342
343
+
<aname="asking-for-confirmation"></a>
334
344
#### Asking For Confirmation
335
345
336
346
If you need to ask the user for a simple 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`.
@@ -339,6 +349,7 @@ If you need to ask the user for a simple confirmation, you may use the `confirm`
339
349
//
340
350
}
341
351
352
+
<aname="auto-completion"></a>
342
353
#### Auto-Completion
343
354
344
355
The `anticipate` method can be used to provide auto-completion for possible choices. The user can still choose any answer, regardless of the auto-completion hints:
@@ -351,6 +362,7 @@ Alternatively, you may pass a Closure as the second argument to the `anticipate`
351
362
// Return auto-completion options...
352
363
});
353
364
365
+
<aname="multiple-choice-questions"></a>
354
366
#### Multiple Choice Questions
355
367
356
368
If you need to give the user a predefined set of choices, you may use the `choice` method. You may set the array index of the default value to be returned if no option is chosen:
@@ -390,6 +402,14 @@ If you would like to display plain, uncolored console output, use the `line` met
390
402
391
403
$this->line('Display this on the screen');
392
404
405
+
You may use the `newLine` method to display a blank line:
406
+
407
+
$this->newLine();
408
+
409
+
// Write three blank lines...
410
+
$this->newLine(3);
411
+
412
+
<aname="table-layouts"></a>
393
413
#### Table Layouts
394
414
395
415
The `table` method makes it easy to correctly format multiple rows / columns of data. Just pass in the headers and rows to the method. The width and height will be dynamically calculated based on the given data:
@@ -400,6 +420,7 @@ The `table` method makes it easy to correctly format multiple rows / columns of
400
420
401
421
$this->table($headers, $users);
402
422
423
+
<aname="progress-bars"></a>
403
424
#### Progress Bars
404
425
405
426
For long running tasks, it could be helpful to show a progress indicator. Using the output object, we can start, advance and stop the Progress Bar. First, define the total number of steps the process will iterate through. Then, advance the Progress Bar after processing each item:
@@ -477,6 +498,7 @@ You may also specify the connection or queue the Artisan command should be dispa
477
498
'user' => 1, '--queue' => 'default'
478
499
])->onConnection('redis')->onQueue('commands');
479
500
501
+
<aname="passing-array-values"></a>
480
502
#### Passing Array Values
481
503
482
504
If your command defines an option that accepts an array, you may pass an array of values to that option:
@@ -487,6 +509,7 @@ If your command defines an option that accepts an array, you may pass an array o
487
509
]);
488
510
});
489
511
512
+
<aname="passing-boolean-values"></a>
490
513
#### Passing Boolean Values
491
514
492
515
If you need to specify the value of an option that does not accept string values, such as the `--force` flag on the `migrate:refresh` command, you should pass `true` or `false`:
@@ -528,3 +551,8 @@ The Artisan console's `make` commands are used to create a variety of classes, s
528
551
php artisan stub:publish
529
552
530
553
The published stubs will be located within a `stubs` directory in the root of your application. Any changes you make to these stubs will be reflected when you generate their corresponding classes using Artisan `make` commands.
554
+
555
+
<aname="events"></a>
556
+
## Events
557
+
558
+
Artisan dispatches three events when running commands: `Illuminate\Console\Events\ArtisanStarting`, `Illuminate\Console\Events\CommandStarting`, and `Illuminate\Console\Events\CommandFinished`. The `ArtisanStarting` event is dispatched immediately when Artisan starts running. Next, the `CommandStarting` event is dispatched immediately before a command runs. Finally, the `CommandFinished` event is dispatched once a command finishes executing.
0 commit comments