How Developers “Play” with Laravel Tinker
What is Tinker?
Laravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.
— Laravel Doc
Laravel Tinker is a tool for interacting with the database without the need to create routes. It is used with “PHP artisan” to create objects or modify data. “PHP artisan” is a command-line interface available in Laravel. Tinker is a command-line tool that works with “PHP artisan”. A Tinker allows you to create objects, insert data into the database, etc.
When to Use Tinker?
When a developer wants to see the results of queries, the fastest way is to do some processing like creating Controllers, routes, Models, etc. However, with php artisan tinker, we can quickly see the output. Tinker allows us to directly interact with the data via our application, using commands like stop() or dd() and even print_r().
How Does Tinker Work?
To get started with Tinker, just open the terminal and run the command php artisan tinker. This will open an interactive command-line environment for you. Here, you can type and run PHP commands to interact with your Laravel application. Try typing the command User::all() to get all the users in your database. You’ll get a list of user objects in your application. It’s simple and fast, right?
php artisan tinker
1. Query database
Query with conditions (multiple conditions):
\App\Models\Post::query() \
->with(‘user’) \
->where(‘count_view’, ‘>’, 200) \
->whereRaw(‘month(created_at) = ?’, [Carbon\Carbon::now()->month]) \
->orderByDesc(‘count_view’) \
->limit(10) \
->get();
Add a new record:
$user = new App\Models\User;
$user->email = “[email protected]“
$user->name = “RCVN”
$user->password = \Hash::make(‘gX6JgDbmNd)97FS*’)
$user->save();
Delete a record from the database:
$user = App\Models\User::where(‘name’, ‘like’, ‘%leone%’)->first();
$user->delete();
2. Write function in Tinker
A function usually consists of multiple lines, but if you press enter, Tinker will interpret that as the end of the command. Therefore, to write a function in Tinker, you can use \ to terminate each line and continue the next line.
function sayHelloName($userId) { \
$user = App\Models\User::find($userId); \
return “Hello {$user->name}”; \
}
> sayHelloName(101)
= “Hello RCVN”
3. Up site/down site in a flash (maintenance mode)
Bring the website down with ease.
After debugging, bring the website back up.
4. View the source code of a function or class
show <functionName>
5. Tinker Commands
Similar to the Artisan command-line application, Tinker provides many commands and utilities that simplify the debugging process and executing code from the command line. The table below lists all the commands Tinker provides (these commands can be explored in Tinker REPL using the help command):
Command | Description | Alias |
help | Display a list of Tinker commands. | ? |
doc | Read documentation for an object, class, constant, method, or property. | rtfm, man |
ls | List local variables, methods, constants, traits, or class. | list, dir |
show | Show the source code for an object, class, constant, method, or property. | |
wtf | Show the backtrace of the most recent exception. | last-exception, wtf? |
whereami | Show where you are in the code. | |
trace | Show the current call stack. | |
throw-up | Throw an exception out of the Psy Shell. | |
buffer | Display (or clear) the input buffer contents. | buf |
clear | Clear the Psy Shell screen. | |
history | Display Psy Shell history. | hist |
exit | End the current session and return to the caller. | quit, q |
clear-compiled | Clear compiled class files. | |
down | Put the application in maintenance mode. | |
env | Display the current environment configuration. | |
optimize | Optimize the framework for better performance. | |
up | Bring the application out of maintenance mode. | |
migrate | Run database migrations. | |
schedule:list | List the scheduled tasks. | |
schedule:run | Run the scheduled tasks. |
Use Laravel Tinker directly in the browser
If you’ve been using Tinker in the terminal, you’ve probably felt the frustration of having to copy/paste long code (like writing functions in Tinker). Well, laravel-web-tinker is exactly what you need to remove that frustration.
Like any other package, you can install it with just a few commands:
composer require spatie/laravel-web-tinker –dev
php artisan web-tinker:install
php artisan vendor:publish –provider=”Spatie\WebTinker\WebTinkerServiceProvider” –tag=”config”
Change the config in the file:
And now you can access localhost:8000/tinker and experience it:
Conclusion
In this article, we introduced the basics of using Tinker on both the terminal and web interface. We hope the article has provided useful information for readers and helps you on your journey to mastering Laravel.
References: Laravel Doc | |
![]() | Hồ Nguyễn Văn Nhật PHP Developer |