| name | tinker-usage |
| description | Laravel Tinker best practices for debugging, testing ideas, and data exploration. When and how to use safely. |
Tinker Best Practices
When to Use Tinker
| Use Tinker | Use Tests Instead |
|---|
| Quick debugging | Repeatable verification |
| Data exploration | Creating production data |
| Testing one-liners | Complex logic validation |
| Checking relationships | Testing side effects |
Safe Usage Rules
Never Modify Production Data
User::where('role', 'admin')->delete();
User::where('role', 'admin')->count();
User::where('role', 'admin')->pluck('email');
Use Database Transactions
DB::beginTransaction();
$user = User::factory()->create();
$user->posts()->createMany([...]);
$user->posts()->count();
DB::rollBack();
Useful Snippets
Relationship Debugging
User::with('posts')->first()->posts;
DB::enableQueryLog();
User::with('posts.comments')->first();
dd(DB::getQueryLog());
Model Inspection
(new Post)->getFillable();
(new Post)->getGuarded();
(new Post)->getCasts();
collect((new ReflectionClass(Post::class))->getMethods())
->filter(fn($m) => $m->class === Post::class)
->pluck('name');
Testing Factories
Post::factory()->make();
Post::factory()->make(['title' => 'Custom']);
Post::factory()->published()->make();
Configuration Check
config('app.name');
config('database.default');
config('queue.default');
Alternatives to Tinker
For Persistent Debugging
- Telescope: Full request/query/job inspection
- Debugbar: Browser-based debugging
- Ray: Desktop debugging app
For Data Operations
- Seeders: Repeatable data creation
- Feature Tests: Verify behavior with assertions
- Artisan Commands: Scripted data operations
Tinker Configuration
Auto-Imported Classes
'alias' => [
'User' => App\Models\User::class,
'Post' => App\Models\Post::class,
],
Dangerous Commands Block
'dont_alias' => [
App\Services\PaymentService::class,
],
IDE Integration
PsySH Configuration
return [
'defaultIncludes' => [
__DIR__.'/tinker_helpers.php',
],
'historySize' => 1000,
];