laravel/tinker
Laravel Tinker provides an interactive REPL for Laravel, letting you run PHP code and interact with your application from the command line via Artisan tinker. Great for debugging, testing ideas, and exploring models, services, and configuration.
Installation: Tinker is included by default in Laravel. No additional installation is required.
First Use: Launch Tinker via:
php artisan tinker
This opens an interactive REPL shell with access to your Laravel application’s container and helpers.
First Use Case: Inspect a model or service immediately:
// Example: Fetch and inspect a user
$user = App\Models\User::find(1);
$user->toArray();
php artisan tinker --help for CLI options like --execute for one-liners.Interactive Debugging
// Simulate a job execution
$job = new \App\Jobs\ProcessOrder($orderId);
$job->handle(new \Illuminate\Bus\Dispatcher($app));
var_dump() or dd() (dump and die) for quick inspection.Model Exploration
// Fetch and update a model
$post = \App\Models\Post::first();
$post->update(['title' => 'Updated Title']);
$post->fresh()->title; // Verify changes
where() or with() directly in the REPL.Service Container Access
// Resolve and test a service
$mailer = app(\Illuminate\Mail\Mailer::class);
$mailer->raw('Test email', function ($message) {
$message->to('test@example.com');
});
One-Liners with --execute
php artisan tinker --execute="App\Models\User::count()"
Custom Helpers
Tinker.php file in your project root with helper functions:
// File: Tinker.php
function u($id) {
return \App\Models\User::findOrFail($id);
}
Testing Logic Snippets
// Test a validation rule
$validator = \Validator::make(['email' => 'test@example.com'], [
'email' => 'required|email'
]);
$validator->passes(); // true/false
.env file, so database connections, queues, and configs are pre-loaded.Artisan::call():
Artisan::call('migrate:status');
event(new \App\Events\OrderPlaced($orderId));
$html = view('emails.welcome', ['user' => $user])->render();
Database Transactions
DB::transaction() to roll back changes:
DB::transaction(function () {
$user = new \App\Models\User();
$user->save();
// Changes rolled back if an exception occurs
});
Queue Jobs
Bus::dispatchSync() for synchronous testing:
Bus::dispatchSync(new \App\Jobs\ProcessOrder($orderId));
PsySH Quirks
composer update laravel/tinker
Model Events
static::withoutEvents():
\App\Models\User::withoutEvents(function () {
$user = \App\Models\User::find(1);
$user->name = 'Test';
$user->save(); // No observers triggered
});
Caching
Artisan::call('cache:clear');
Artisan::call('config:clear');
dump() (from Laravel) or var_export() for detailed output.history to revisit past commands or !! to repeat the last command.Psy\Configuration::getInstance()->addCustomCaster(
\App\Models\User::class,
function ($user) {
return "User#{$user->id}: {$user->name}";
}
);
Custom REPL Configuration
config/tinker.php (if it exists) or via Psy\Configuration:
Psy\Configuration::getInstance()->setOption('colors', false);
Pre-Loaded Code
Tinker.php file to auto-load helpers, aliases, or setup code:
// Tinker.php
use App\Models\User as AppUser;
alias('User', AppUser::class);
Custom Commands
Psy\Command\Command::add('list:users', function () {
return \App\Models\User::all()->pluck('name');
});
Environment-Specific Setup
if (app()->environment('local')) {
\App\Models\User::factory()->create(['email' => 'local@example.com']);
}
exit or Ctrl+D.Bus::fake() for assertions:
Bus::fake();
Bus::dispatch(new \App\Jobs\ProcessOrder($orderId));
Bus::assertDispatched(\App\Jobs\ProcessOrder::class);
How can I help you explore Laravel packages today?