Stop refreshing your browser to test code. Learn how to use Laravel Tinker to run Eloquent queries and debug your application logic directly in the terminal.
Previously in this course, we explored Introduction to Artisan Commands to manage our application via the command line. In this lesson, we are diving into one of the most powerful tools in your Laravel toolkit: Tinker.
Tinker is a REPL (Read-Eval-Print Loop) for your Laravel application. It allows you to interact with your entire application environment—including your models, database, and services—directly from your terminal. Instead of writing code, saving a file, and refreshing your browser to see if a query works, you can execute code live and see the results instantly.
When you’re building your Task Manager, you often need to verify that your Eloquent relationships are set up correctly, as discussed in Introduction to Database Relationships in Laravel, or confirm that your Updating Existing Records: A Laravel CRUD Guide logic behaves as expected.
Tinker gives you a sandbox environment. You don't have to worry about broken views or routing issues; you are talking directly to your application's heart.
To launch Tinker, open your terminal in your project root and run:
Bashphp artisan tinker
Once the prompt changes to >>>, you are inside your application. Every line of PHP code you type here has access to your models, configuration, and helpers.
Let’s interact with our Task model. You can fetch all tasks, filter them, or create new ones just as you would in a controller.
PHP#6A9955">// Fetch all tasks $tasks = App\Models\Task::all(); #6A9955">// Find a specific task by ID $task = App\Models\Task::find(1); #6A9955">// Filter tasks for a specific user $userTasks = App\Models\Task::where('user_id', 1)->get();
You can test your validation and model logic without ever submitting a form. For instance, if you want to verify that your mass-assignment protection is working or test a new status update:
PHP#6A9955">// Create a new task $task = new App\Models\Task(); $task->title = 'Learn Tinker'; $task->user_id = 1; $task->save(); #6A9955">// Update an existing task $task = App\Models\Task::find(1); $task->status = 'completed'; $task->save();
Imagine you want to ensure that your Task Manager correctly handles a specific logic requirement, such as counting how many incomplete tasks a user has. Instead of building a view, run this in Tinker:
php artisan tinker.$user = App\Models\User::first();$count = $user->tasks()->where('status', 'pending')->count();This is the fastest way to debug complex queries before you finalize your Querying Related Data: Mastering Eager Loading in Laravel implementations.
php artisan tinker.Task model to create a new task directly from the CLI.Task::latest()->first() to verify it was saved to the database.$task->delete() method.App\Models\Task). If you get a "Class not found" error, check the namespace.$task->title = 'New Title' only exist in memory until you call $task->save(). If you exit Tinker without saving, your changes are lost.delete() commands!Tinker is your bridge between the terminal and your application logic. By using it to run Eloquent queries, manipulate models, and test snippets of code, you drastically reduce your feedback loop. It is an essential skill for any developer looking to move beyond simple browser-based testing.
Up next: We will explore how to use Service Providers to organize our application logic as it grows in complexity.
Master the Artisan CLI to automate repetitive tasks and debug your Laravel application. Learn how to list, create, and run custom commands today.
Read moreStop the N+1 query problem in its tracks. Learn how to use eager loading in Laravel to keep your application fast and efficient as your data grows.
Debugging with Laravel Tinker
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation