Both Laravel and Drupal have an interactive debugger & REPL environment for tinkering in PHP. You can test various php statements, resolve services or even query the database! Both environment's are customized versions of the excellent PsySh.
-
Laravel:
php artisan tinker
-
Drupal via a Drush:
drush php
This new command, ddev tinker
, checks which project you are currently in, and runs the correct command for the project. If you bounce between projects types, this command is really useful.
This command works best if installed globally (set-and-forget). It can then be accessed in any Laravel or Drupal project.
- Copy the web/tinker file into
~/.ddev/commands/web
.
You could also use this command on a per-project basis.
- Copy the web/tinker file into your project
./.ddev/commands/web
.
To start your framework's REPL environment, simply type the follow:
ddev tinker
Arguments are currently supported (with caveats ) in Laravel projects only.
$ ddev tinker 6+8
14
$ ddev tinker "'User::first()'"
[!] Aliasing 'User' to 'App\Models\User' for this Tinker session.
App\Models\User^ {#4400
...
There is known issue when arguments contain:
- quotes (
"
,'
) - or brackets (
(
,)
)
There are 2 known workaround:
- escape the character:
ddev tinker "User::find(1)"
=>ddev tinker "User::find\(1\)"
- double-wrap; use a different matching pair of quotes:
ddev tinker "User::find(1)"
=>ddev tinker '"User::find(1)"'
ddev tinker "User::find(1)"
=>ddev tinker "'User::find(1)'"
While this might be helpful for a quick one-off command, it's recommend to run ddev tinker
for tinkering to avoid any Docker connection delays between multiple commands.
Contributed by @tyler36