Laravel Tinker
Tinker is a console that runs PHP code inside your app. It lets you try code without a route or a page.
Simple maths
Tinker prints the answer to a short line.
php artisan tinker --execute="echo 2 + 3;"A string helper
Any PHP code runs the same way.
php artisan tinker --execute="echo strtoupper('laravel');"Counting users
No users exist yet, so the count is zero.
php artisan tinker --execute="echo \App\Models\User::count();"Example
php artisan tinker --execute="\$t = \App\Models\Task::create(['title' => 'Buy milk', 'done' => false]); echo \$t->title;"Key points
- Tinker: a console that runs PHP code inside your app.
- Use tinker to try a short line of code.
- Use tinker to read and change database rows.
- Use --execute to run one line and exit.
In this example, tinker runs one line inside the app. The line creates a task with a title. echo prints the title back. Tinker is a console that runs PHP code inside your app. Run the line in your own app copy to see the output.
Note
Tinker runs real code, so a change it makes stays changed.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does tinker do?