Background

At work, due to business adjustments, data structures and models often need to be modified. Initially, I created Commands to solve similar requirements, but later found that the number of Commands kept increasing, and they were all logic used only once.

So I thought of executing data structure adjustment logic directly in Laravel Tinker on the server.

Basic Usage

$ php artisan tinker

This allows you to enter the Tinker Shell, where we can execute some code within the Laravel framework. However, it is a REPL—an interactive interpreter, and normally we can only execute one line of command at a time. For example:

>>> use Illuminate\Support\Str;
>>> Str::random(32);
=> "WAjGwmSqXqzQ6ZWerUR6GRvNGW4wWjPA"
>>>

Configuration

In the interaction with Tinker above, we can see that to use some Classes, we must input the complete class namespace, which is maddening for someone like me who is used to PHPStorm. Previously, I would write the code in PHP Storm first and then paste it in.

Later, I discovered that Tinker has a configuration file, but it’s not included in the project by default. We need to use the following command to publish the configuration file to the project’s configuration directory:

$ php artisan vendor:publish --provider='Laravel\Tinker\TinkerServiceProvider'

The resulting config/tinker.php file content is as follows:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Console Commands
    |--------------------------------------------------------------------------
    |
    | This option allows you to add additional Artisan commands that should
    | be available within the Tinker environment. Once the command is in
    | this array you may execute the command in Tinker using its name.
    |
    */

    'commands' => [
        // App\Console\Commands\ExampleCommand::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Auto Aliased Classes
    |--------------------------------------------------------------------------
    |
    | Tinker will not automatically alias classes in your vendor namespaces
    | but you may explicitly allow a subset of classes to get aliased by
    | adding the names of each of those classes to the following list.
    |
    */

    'alias' => [
        'Str' => 'Illuminate\\Support\\Str'
    ],

    /*
    |--------------------------------------------------------------------------
    | Classes That Should Not Be Aliased
    |--------------------------------------------------------------------------
    |
    | Typically, Tinker automatically aliases classes as you require them in
    | Tinker. However, you may wish to never alias certain classes, which
    | you may accomplish by listing the classes in the following array.
    |
    */

    'dont_alias' => [
        'App\Nova',
    ],

];

We can set aliases for the classes we use in the alias section, so that we can directly use Str in Tinker.

$ php artisan tinker

Psy Shell v0.11.2 (PHP 8.1.4 — cli) by Justin Hileman
>>> Str::random();
[!] Aliasing 'Str' to 'Illuminate\Support\Str' for this Tinker session.
=> "gHiy1WFn64IuKf7j"
>>>

You can see that Tinker will prompt Aliasing 'Str' to 'Illuminate\Support\Str' for this Tinker session.

Edit Mode

As you can see, executing single-line code is quite convenient, but executing multi-line code can be troublesome.

At this point, you can use the edit command to open the editor mode, which will launch the system’s built-in editor, which could be vi or nano.

Editing temp code in vi mode

Although this method can solve our problem of executing multi-line code, a new problem arises: having to write the code in PHPStorm first and then paste it in is still not comfortable.

IDE Plugin

So I searched the PHPStorm Plugins Store and found that there is indeed a Laravel Tinker plugin.

Laravel Tinker Plugin

After installation, you need to set the project’s root directory, as follows:

Laravel Tinker Plugin Settings

Then you can happily execute code in this plugin, and it supports code auto-completion.

Laravel Tinker Plugin Autocomplete

Other Solutions

Tinkerwell

After using the plugin, I discovered another problem: I need to write in the plugin and then paste it into the Tinker on the online server to execute, which is not elegant enough.

After some searching, I found Tinkerwell online, a GUI tool that supports directly executing code in the editor on remote projects, and supports creating code snippets for multiple uses.

I have to say the UI is great, but there are two fatal problems:

  1. The price of $29.9/year, which discourages many people;
  2. It does not currently support PHP environments inside Docker containers. The current solution is to start an SSH Server daemon inside the container.

Tinkerun

Tinkerun is an open-source project with functionality similar to the commercial version of Tinkerwell, but the implementation principle seems different.

According to the author, Tinkerun uses node-pty to simulate executing php artisan tinker and then uses xterm.js to send the edited code to the remote Tinker for execution. The advantage is that it can support PHP environments inside Docker containers.

The settings page is very simple, mainly the Command configuration item:

Tinkerun Settings

I hope this is helpful, Happy hacking…