Laravel

How to use Laravel Tinker for Database Operations?

February 28, 2018

author:

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:

doc route
Laravel Artisan Tinker doc command

We can also see the basic scaffolding of methods and functions like this:

show route
Laravel Artisan Tinker show command

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
Laravel Artisan Tinker Migrate Database

Now run tinker command in the console from the root directory of the project.

php artisan tinker
Laravel Artisan Tinker Command

# 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:

factory(App\User::class, 8)->create();
Laravel Artisan Tinker Seed Database Table

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 = "khushboo@gmail.com";
$user->password = bcrypt('secret');
$user->save();
Laravel Artisan Tinker Insert in Database

• Aggregate – Count:

App\User::count();
Laravel Artisan Tinker Record Count

• Edit Operation:

$user = App\User::find(1);
$user->update(['name'=>'Hashvel']);
Laravel Artisan Tinker Edit Database Record

• Delete Operation:

$user = App\User::find(5);
$user->delete();
Laravel Artisan Tinker Delete Database Record

# 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' => 'khushboo@gmail.com', 'password' => 'secret'])

Note that you can also login using the id:

auth()->loginUsingId(5)
auth()->id()

Conclusion:
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.

Leave a comment

Your email address will not be published. Required fields are marked *