How to use Tinker in Laravel Application

2618

Tinker is a beautiful laravel package. Laravel Tinker is a powerful REPL for the Laravel framework. REPL stands for Read–Eval–Print Loop. This feature is powered by PsySH console.

The main function of tinker is to allow you to directly interact with laravel application from the command line such as Eloquent ORM, jobs, events, and more. In this article, we will do CRUD operations with tinker. It comes default with laravel. But, you may use it in other framework projects by installing it via composer. To install it, run the below command:

composer require laravel/tinker

Database Setup

Before we get started playing with tinker, we need to setup a database for our application. Let’s create a new database called kodementor and do necessary configurations in .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=kodementor
DB_USERNAME=root
DB_PASSWORD=secret

Migration

After database setup, we will migrate our default migration file for users table. To do so, run the artisan command:

php artisan migrate

Now, we can start playing with tinker to interact with our database. Please note that tinker is case sensitive. To enter in tinker environment, we need to run artisan command:

php artisan tinker

Seeding Database

Seeding database is one of the most frequently used technique using tinker. Using this method, we can populate our database in a single command using a factory. The following command will seed our database with 20 rows of data in our App\User model. Isn’t it nice?

factory(App\User::class, 20)->create();

seeding database with tinker

Accessing Database

After seeding, let’s access our users’ list. Simply write the code as you used to write in IDE.

App/User::all();

You can also limit the number of records while accessing it from the database.

>>> App\User::limit(4)->get();
// output
=> Illuminate\Database\Eloquent\Collection {#2886
     all: [
       App\User {#2887
         id: 1,
         name: "Chancellor Salas",
         email: "vowutedovu@hotmail.com",
         phone: null,
         created_at: "2018-08-24 11:43:22",
         updated_at: "2018-08-24 11:43:22",
       },
       App\User {#2888
         id: 2,
         name: "Caryn Fox",
         email: "xyzosyb@gmail.com",
         phone: null,
         created_at: "2018-08-24 11:59:19",
         updated_at: "2018-08-24 11:59:19",
       },
       App\User {#2889
         id: 3,
         name: "Prof. Winston Langworth V",
         email: "gus52@example.net",
         phone: null,
         created_at: "2018-08-25 10:45:17",
         updated_at: "2018-08-25 10:45:17",
       },
       App\User {#2890
         id: 4,
         name: "Pattie Hansen",
         email: "estell34@example.net",
         phone: null,
         created_at: "2018-08-25 10:45:18",
         updated_at: "2018-08-25 10:45:18",
       },
     ],
   }
>>>

Also, you can retrieve only first record. Run the command:

>>>App/User::first();
// output
=> App\User {#2967
     id: 1,
     name: "Chancellor Salas",
     email: "vowutedovu@hotmail.com",
     phone: null,
     created_at: "2018-08-24 11:43:22",
     updated_at: "2018-08-24 11:43:22",
   }

Moreover, you can find any record using Id. Below code will return a user with id of 10.

App\User::find(10);

Insert Record Manually

We used the factory to seed our database before, but we can do it manually as well. For this, we need to get the model and store it in a variable. Then, assign the attribute on the variable. After all necessary attributes have been set, we can finally call the save method.

>>> $user = new App\User;
// output
=> App\User {#2874}
>>> $user->name = 'vijay';
//output
=> "vijay"
>>> $user->email = 'vijay@kodementor.com';
// output
=> "vijay@kodementor.com"
>>> $user->password = bcrypt('secret');
// output
=> "$2y$10$poVkJRccbSIwyKwu2/mdAOMyTw4GZhJsig5MQadS9SaEBLBCXTB1K"
>>> $user->save();
// output
=> true
>>> $user   //displaying recently inserted user
// output
=> App\User {#2874
     name: "vijay",
     email: "vijay@kodementor.com",
     updated_at: "2018-08-25 11:21:39",
     created_at: "2018-08-25 11:21:39",
     id: 21,
   }

Updating Database

You can also update any information from database. For this, get the record, assign new attribute and save to database.

>>> $user = App\User::find(23);
// output
=> App\User {#2867
     id: 23,
     name: "vijay",
     email: "vijay@kodementor.com",
     phone: null,
     created_at: "2018-08-25 11:21:39",
     updated_at: "2018-08-25 11:21:39",
   }
>>> $user->name = 'vijay rana';
// output
=> "vijay rana"
>>> $user->save();
// output
=> true
>>> $user
// output
=> App\User {#2867
     id: 23,
     name: "vijay rana",
     email: "vijay@kodementor.com",
     phone: null,
     created_at: "2018-08-25 11:21:39",
     updated_at: "2018-08-25 11:28:42",
   }
>>>

Deleting Record

Obviously, we can also delete record using this package.

>>> $user = App\User::find(23);
// output
=> App\User {#2883
     id: 23,
     name: "vijay rana",
     email: "vijay@kodementor.com",
     phone: null,
     created_at: "2018-08-25 11:21:39",
     updated_at: "2018-08-25 11:28:42",
   }
>>> $user->delete();
// output
=> true
>>> App\User::find(23);
// output
=> null
>>>

Reading Documentation

With the help of tinker, you can even read functions documentation. For this, we need to type doc before function name. Let’s try reading Laravel helper function in laravel.

>>> doc request
// output
function request($key = null, $default = null)

Description:
  Get an instance of the current request or an input item from the request.

Param:
  array|string  $key
  mixed         $default

Return:
  \Illuminate\Http\Request|string|array
>>>

You can also read PHP documentation but you need to download it in local. You can read on about PHP manual installation.

Conclusion

So, this is all about using tinker in laravel application. You can do a lot more besides the above-mentioned steps. For this, simply type help command so that you can see the capabilities built-in to tinker.

LEAVE A REPLY

Please enter your comment!
Please enter your name here