PHPStorm: Scratch Files as Tinker

Search for a command to run...

No comments yet. Be the first to comment.
A few months ago, my product manager dropped something in the team channel. "We are adding an AI agent to the platform next quarter. It will handle follow-ups automatically." Just like that. One messa

f you’re diving into React, you’ve likely come across the ‎useRef hook. But what exactly is ‎useRef(), and how can it make your life easier? In this post we’ll cover what ‎useRef is, walk through five

A bug that only happens for some users, that you can never reproduce, that isn't in your code. Welcome to the React + browser-translation crash — what causes it, why it's so sneaky, and the battle-tested one-file fix.

Upgrading a core production database is usually the stuff of dev nightmares. It often involves maintenance windows, scheduled downtime, and the looming fear of a botched migration. But what if you cou

We are moving into products that adapt, predict and react with real context. Designing only interfaces is not enough anymore. We are designing experiences that understand the user. Designers and digital agencies are now challenged to move beyond trad...

Engineering at JoBins
167 posts
Welcome to Engineering at JoBins — where we share the stories, insights, and lessons from building a world-class hiring platform. From system design and scalability to developer tools and team culture, our engineers write about the real challenges we solve every day. Whether you're a curious developer or a fellow builder, we hope our experiences inspire and inform your own engineering journey.
As a developer, we often need quick test code snippets, library exploration, or debugging specific application parts. Observing the history of such tasks, Laravel's Tinker seems the most preferred choice.
However, using PHPStorm as an IDE for development, one can leverage the ability of scratch files to achieve alike functionality with greater convince and get the benefits of an integrated environment.
In this blog, we will explore how to make use of PHPStorm Scratch Files in an effective way simulating the tinker-like experience with examples.
We will be using Laravel 's reference for the simulation of scratch file power. Here is the directory structure and some initials code setup you might have:
/new-laravel-project
|-- app
|-- bootstarp
|-- config
|-- database
|-- public
|-- resources
|-- routes
|-- storage
|-- tests
|-- vendor
Open PHPStorm
Go to File -> New Scratch File -> PHP
A new scratch file will be created with the name something like this 'scratch.php' under Scratches and Consoles -> Scratches

Copy and paste the following code into your scratch file:
const BASE_PATH = '/new-laravel-project'
require_once BASE_PATH.'/vendor/autoload.php'
use Illuminate\Foundation\Console\Kernel;
$app = require BASE_PATH.'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
//Your code goes here
Now you are free to write any code you want to test within this file. For example, if you want to test a simple database query, something like this can be done:
$user= \App\Models|User::find(1);
echo $user->name;
Right-click on the scratch file and select Run scratch.php
PHPStorm will execute the script, the output can viewed from the Run tool window:

PHPStorm provides a powerful, integrated development environment with features like autocompletion, debugging, and other advanced features to enhance productivity
it is easy to create scratch files without affecting the main codebase. They remain ideal for serving with experiments and temporary testing purposes only.
The entire project context is accessible in scratch files, which helps the developer test any part of the application without the need for separate scripts or environments.
While Tinker serves as a great REPL(READ-EVAL-PRINT-LOOP), it might not always integrate well with the IDE. Whereas, Scratch files can fulfill that part with interactive experience co-working with PHPStorm's features.
https://www.jetbrains.com/guide/javascript/tips/create-scratch/
https://www.digitalocean.com/community/tutorials/what-is-repl
To conclude, we can expect a versatile and powerful way of testing and debugging PHP code within a familiar environment with PHPStorm Scratch files. By Laravel project context setup in a scratch file, the developer can effectively use it like with Tinker, instantly aiding in rapid experimentation and problem-solving.
Try incorporating scratch files into your project workflow and experience the seamless integration and efficiency brought to your development process.
I hope this blog was helpful 😊. Please feel free to ask me any other questions you may have. I am always happy to help 🤗.
Happy coding with PHPStorm.