0

Tinker — Laravel's REPL for running code against your app live

Beginner5 min read·eng-14-013

Concept

Tinker — Laravel's REPL (Read-Eval-Print Loop) for running PHP code interactively against your full application. "Tinker" means to make small adjustments or experiment casually.

What REPL means: Read → Eval → Print → Loop. You type code, it runs, shows the result, waits for more. Like a PHP shell but with your app bootstrapped — models, services, database, config all available.

How it works: php artisan tinker boots the Laravel application (runs service providers, loads config, establishes DB connection) and opens a PsySH shell (a PHP REPL package). Everything available in your app is available in Tinker.

What you can do in Tinker:

  • Query the database: User::count(), User::where('active', true)->get().
  • Create records: User::create(['name' => 'Test', 'email' => 'test@x.com', 'password' => 'hashed']).
  • Call services: app(OrderService::class)->createOrder([...]).
  • Check config: config('app.key'), config('mail.mailers').
  • Run migrations (rarely): Artisan::call('migrate').
  • Debug: Inspect output of any code path.

PsySH features (the underlying engine):

  • ls — list available variables and functions.
  • doc User — shows PHP docblock for a class.
  • show User::boot — shows source code of a method.
  • Arrow key history — recall previous commands.
  • Tab completion.

Security: Tinker runs with FULL app access including DB write access. Never run php artisan tinker on production servers — it bypasses all authorization.

Code Example

bash
# Start Tinker
php artisan tinker
php
// Inside Tinker — all of these work:

// Query the database
>>> User::count()
=> 42

>>> User::where('active', true)->with('roles')->first()
=> App\Models\User {#...}

>>> User::factory()->create(['email' => 'test@example.com'])
=> App\Models\User {#... "email": "test@example.com"}

// Use facades and helpers
>>> Cache::get('config')
=> null

>>> now()->addDays(7)
=> Illuminate\Support\Carbon {..."2026-06-21T..."}

>>> str('Hello World')->slug()
=> Illuminate\Support\Stringable {#... "hello-world"}

// Call services from the container
>>> app(App\Services\OrderService::class)
=> App\Services\OrderService {#...}

// Run any PHP
>>> collect([1,2,3])->map(fn($x) => $x * 2)->sum()
=> 12

// Dump and inspect
>>> $user = User::first()
>>> $user->toArray()
=> ["id" => 1, "name" => "Alice", ...]

// Call Artisan commands from within Tinker
>>> Artisan::call('cache:clear')
=> 0

// Use helpers
>>> bcrypt('password')
=> "$2y$12$..."

>>> Str::uuid()
=> "550e8400-e29b-41d4-a716-446655440000"
php
<?php
// You can also run one-off code snippets using --execute:
// php artisan tinker --execute="echo User::count();"

// Useful for scripts during deployments:
// php artisan tinker --execute="App\Models\Setting::updateOrCreate(['key' => 'maintenance'], ['value' => '0']);"