laravel/tinker
Laravel Tinker provides an interactive REPL (powered by PsySH) for Laravel applications. Quickly run PHP and Artisan code, inspect models and services, and debug in a live console with your app’s full context loaded.
php artisan tinker
>>> $user = App\Models\User::first();
>>> $user->name
php artisan tinker --help to explore available options like --execute for one-off commands.Debugging a Model Query:
>>> $posts = App\Models\Post::where('published', true)->get();
>>> $posts->count()
10
>>> $posts->first()->title
"My First Post"
Interactive Debugging:
>>> $user = App\Models\User::find(1);
>>> $user->posts()->where('views', '>', 1000)->get()
Testing Logic Snippets:
>>> $validator = Validator::make(['email' => 'test@example.com'], ['email' => 'required|email']);
>>> $validator->passes()
true
One-Off Commands:
--execute to run a command and exit:
php artisan tinker --execute="App\Models\User::all()->count()"
Service Container Inspection:
>>> app()->bound('cache')
true
>>> app('cache')->store()
"file"
Custom Casters:
HtmlString or Stringable).Money class:
>>> Psy\Output\OutputInterface::getCasters()->add('App\Money', function ($money) {
... return '$' . $money->getAmount();
... });
.env variables, configured services).Tab to autocomplete classes, methods, and properties (e.g., App\Models\U → App\Models\User).~/.psysh_history (or project-specific if configured).exit or press Ctrl+D to leave Tinker.Database Transactions:
DB::beginTransaction() and DB::rollBack() carefully.>>> DB::beginTransaction();
>>> App\Models\User::create(['name' => 'Test']);
>>> DB::rollBack(); // Undo the creation
Memory Usage:
User::all()) can consume significant memory. Use cursor() for lazy loading:
>>> $users = App\Models\User::cursor();
>>> foreach ($users as $user) { ... }
PsySH Prompts:
trust project prompts by ensuring your composer.json has correct autoloading or use --no-interaction:
php artisan tinker --no-interaction
Exit Code Issues:
--execute for scripts where exit codes matter.Whitelisted Commands:
migrate:fresh) are restricted for security. Check the whitelist or use --execute to bypass.var_dump(), print_r(), or dd() (dumps and dies) for deep inspection.
>>> dd($user->toArray());
APP_DEBUG=true in .env for full error traces.php artisan optimize:clear
Psy\Shell::getInstance()->debug() to inspect PsySH internals.Custom Casters:
Carbon instance:
Psy\Output\OutputInterface::getCasters()->add('Carbon\Carbon', function ($carbon) {
return $carbon->format('Y-m-d H:i:s');
});
Aliases:
~/.psyshrc.php or project’s bootstrap/app.php:
Psy\Configuration::getInstance()->addAliases([
'u' => 'App\Models\User',
'p' => 'App\Models\Post',
]);
Environment Setup:
bootstrap/app.php:
$app->booting(function () {
if ($this->runningInConsole() && php_sapi_name() === 'cli') {
$app->make('Psy\Configuration')->setOption('historyFile', storage_path('tinker_history'));
}
});
Integration with Tests:
phpunit.xml for interactive debugging during tests:
<env name="APP_DEBUG" value="true"/>
; to chain commands (e.g., User::first(); $user->posts;).Shift+Enter for multiline code blocks.How can I help you explore Laravel packages today?