Laravel is a well-known PHP framework with beautiful syntax. One of the tools provided by Laravel is Laravel Tinker, which is a fantastic REPL (Read-Eval-Print Loop) allowing developers to interact from the command line to their Laravel application.

No matter if you are testing Eloquent queries, checking for records in your database, or debugging your relationships, Tinker provides a fast and effortless way of working with and developing your Laravel app. This blog post will provide you with enough information to understand what Laravel Tinker is, and how to use Laravel Tinker.
What is Laravel Tinker?
Laravel Tinker is an interactive REPL (Read-Eval-Print Loop) that ships with Laravel to make the process of utilizing your Laravel app from the command line much easier. Tinker allows a developer to run database queries, test models, run Artisan commands, and run arbitrary PHP code in the context of your Laravel app all without having to create a new page.
How to Use Laravel Tinker?
Step 1: Install Laravel
composer create-project "laravel/laravel:^11.0" laravel-tinker-demo
Step 2: Enter Laravel Tinker
php artisan tinker
Step 3: Creating a Model Instance
Let’s say you have a Post model. You can use Tinker to interact with it directly by creating a new model instance, setting attributes, and saving it to the database.
$post = new App\Models\Post;
$post->title = 'My first post';
$post->content = 'This is the content of the post';
$post→save();
Step 4: Retrieving Data from the Database
You can retrieve records using Eloquent very easily.
$post = App\Models\Post::first();
$post→title;
Step 5: Updating a Record
To update an existing record, you must first retrieve the record, update the necessary attributes, then save the record to the database.
$post = App\Models\Post::first();
$post->title = 'Updated title';
$post→save();
Step 6: Deleting a Record
If you would like to delete a post from your database, you can use the delete() method:
$post = App\Models\Post::first();
$post→delete();
Step 7: Querying Models
You can run complex queries right from Tinker:
$posts = App\Models\Post::where('title', 'LIKE', '%first%')->get();
$posts->count();
Conclusion
Laravel Tinker is a great tool to assist in making your dealings with your application’s data, and testing simpler and faster. As technology continues to advance, developers can keep on attempting and debugging their operations to their apps models, Artisan commands, and database queries interactively.

FAQ
- What is Laravel Tinker?
Laravel Tinker is a Read-Eval-Print Loop (REPL) for Laravel powered by PsySH. It allows developers to run PHP code with the context of their Laravel application through the command line, making it easier to test, debug, and prototype.
- What is Laravel Tinker used for?
Laravel Tinker is used to interact with your Laravel application via the command line. This can involve testing Eloquent queries, creating or editing records, calling functions, and debugging code, all without writing scripts or routes.
- How do I start Laravel Tinker?
You can start Tinker by simply running the following command from your terminal:
php artisan tinker
- How do I exit Laravel Tinker?
You can exit Tinker by either typing exit or pressing Ctrl + D or Ctrl + C.