$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[July 25, 2026] Laravel

Laravel Tinker Well: Debugging

Laravel Tinker Well: Debugging

────────────────────────────────────────────────────────

Laravel Tinker Well: Debugging

Tip: Interactive REPL

php artisan tinker

Run PHP code in the context of your Laravel app.

Gotcha: Auto-Import Models

Tinker auto-imports your models. Just type User::all().

Tip: Dump Variables

dump(User::first());

Gotcha: History

Tinker remembers your command history. Use arrow keys to navigate.

Tip: --execute Flag

php artisan tinker --execute="echo User::count();"

Run a single command without entering REPL.

Gotcha: Session Not Available

Tinker runs outside HTTP context. No session or request data available.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Tinker is the most underrated tool in Laravel's arsenal. I use it daily for debugging, data fixes, and exploring code behavior. The pattern I recommend: keep a tinker.php file in your project root with common debugging snippets — model lookups, cache checks, config values. It's saved me hours of dd() cycles. Also, php artisan tinker with the PsyShell config lets you define custom aliases, making repetitive debugging tasks one-liners.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts