DEV Community

Cover image for "Have you met Tinker?"

Posted on

"Have you met Tinker?"

Good morning everyone and happy MonDEV! ☕
Today, let's go back into the Laravel world to discuss a tool that is always installed with our project but is often underestimated and little known. This is a shame because it's an excellent tool for experimenting with code, conducting tests, or getting quick feedback. I'm talking about Tinker, a REPL (Read-Eval-Print-Loop) based on PsySH. Essentially, what Tinker does is read the commands launched in the console, execute them in the context of our application, and then return the output directly to the console.

This means that, for example, we can perform our Eloquent queries directly from the console to quickly get an idea of the result and how to improve them. You can run a function from the terminal or even write it during the execution of Tinker and then execute it as many times as you want. Or, you can log in a user, who will then be kept in the session, allowing us to perform any functions or queries that require the user to be logged in to work properly.

So, how do we start this tool? From any of your Laravel projects, simply type

php artisan tinker

Beyond the quick use cases mentioned above, there are other hidden gems in this tool:

  • doc [php_function]: in the terminal output, you'll have the documentation for the requested PHP function.
  • wtf will show you the backtrace of the last raised exception.
  • show allows you to examine the code of a class or object that you have saved in a variable.
  • hist lets you view the history of executed commands, taking only a portion with parameters like --tail, --head, or --show. Then, adding the --replay option allows you to re-run the selected portion of code (perhaps written in a previous session).

In short, there are really many options offered by this somewhat underappreciated REPL in the Laravel universe. The choice of how and how much to use it is up to you! Did you already know about it? Have you ever used it?
If not, you have a week ahead to give it a try 😉

As always, have a great Monday and Happy Coding 0_1

Top comments (0)