How to use Laravel Tinker for Database Operations?
Under the hood, Laravel includes Tinker which is a robust REPL console. This Tinker console provides a command line interactive shell to interact with Laravel application. REPL, which stands for Read-Eval-Print-Loop accepts a single user input, processes it and returns the result to the user via a console. This provides an excellent and clean interface to interact with Laravel Framework features including Eloquent ORM and services like events, jobs, etc.
Though this feature is lesser-known in the community, it can prove to be very helpful at times when we quickly want to test the services or modify database data. To do so, we can use artisan command:
php artisan tinker
# Documentation Command
The tinker doc command is a neat way to quickly look for some documented information about a method or function. For instance:
We can also see the basic scaffolding of methods and functions like this:
Let us now dive into some of the most useful parts of Tinker, i.e., manipulating database. To do so, create a fresh Laravel project and run migration:
laravel new learnTinker
.env configs for MySQL connection:
# .env ... DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=blog DB_USERNAME=root DB_PASSWORD=
php artisan migrate
Now run tinker command in the console from the root directory of the project.
php artisan tinker
# Tinker Factory Command
Laravel provides a special model factory that can be used to seed database tables. It can seed dummy records with a single tinker command:
You would see a long list collection of users created in your terminal. (You can, of course, cross-verify in the database also)
# CRUD using Tinker
We can now use Eloquent ORM Queries on this
User model to run various operations:
• Insert Operation – Create New User:
$user = new App\User; $user->name = "Khushboo Shah"; $user->email = "email@example.com"; $user->password = bcrypt('secret'); $user->save();
• Aggregate – Count:
• Edit Operation:
$user = App\User::find(1); $user->update(['name'=>'Hashvel']);
• Delete Operation:
$user = App\User::find(5); $user->delete();
# Tinker Auth
Tinker also has an additional feature to test the application with auth logins. Meaning, if your application has multiple user roles, you can test them from tinker.
Here’s how you can login using specific email:
attempt(['email' => 'firstname.lastname@example.org', 'password' => 'secret'])
Note that you can also login using the
Tinker is a language feature in Laravel Framework which helps the developers to access the application services and run necessary commands quickly. It can also be useful at times to check method information and use during the development process.
Here, we learned its basics. We also created a Laravel application and worked with database Eloquent queries for basic CRUD operations. Use this Laravel feature as much as possible to test the application quickly.
Questions & Comments:
Thank you for stopping by. Hope you found this article useful and can utilize Laravel Tinker methods efficiently. If you have suggestions or questions please share them in the comment section below.