Laravel Artisan Tinker : write command on multiple lines
Asked Answered
H

3

5

How can I write a command on two lines with Laravel Artisan Tinker ?

User::whereEmail('[email protected]')
->get()

PHP Parse error: Syntax error, unexpected T_OBJECT_OPERATOR on line 1

Hedden answered 30/3, 2022 at 5:25 Comment(1)
Does this answer your question? How to write in new line with laravel tinkerFaucet
C
12

Use the \ character to force the REPL into multi-line input mode:

>>> User::whereEmail('[email protected]') \
... -> get()
Commercial answered 30/3, 2022 at 5:45 Comment(0)
B
14

You can type edit, which launches an external editor and loads the generated code into the input buffer.

Binocular answered 6/12, 2022 at 3:40 Comment(1)
Wow this is huge! What a great tip 👏Leucocytosis
C
12

Use the \ character to force the REPL into multi-line input mode:

>>> User::whereEmail('[email protected]') \
... -> get()
Commercial answered 30/3, 2022 at 5:45 Comment(0)
D
0

If I'm going to write several lines of code, I usually prefer a dedicated 'experiment' route over tinker. It becomes somewhat Tinkerwell like.

Route::get('/experiment', function () {
    User::whereEmail('[email protected]')
        ->get();
});

With Xdebug set up, you can watch variables, trace the code line by line etc.

If you are concerned about publishing the experiment route, you may block it in the production with something like the following line before your experiment code:

Route::get('/experiment', function () {
    app()->isProduction() && abort(403, 'This route is only for experimenting.');

    return User::whereEmail('[email protected]')
        ->get();
});

Note that I've also added a return. It is useful if you don't bother using a debugging tool (like Xdebug). You can easily see the return value where you trigger the route from.

Diatessaron answered 20/12, 2023 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.