How To Use Tinker To Store Data in Database In Laravel 6

Today’s topic is How To Use Tinker To Store Data in Database In Laravel 6. Tinker is a useful tool for insert entry in Database Which is we learn in this blog post.

In all Laravel version, we can use this method for insert entry using Laravel Tinker. That is a very useful tool because we can insert data in the database table without using a form or manual entry in the table. 

The best practice to insert data in the table using tinker because if we enter a manual entry in the database we can not entre a Bcrypt password directly in the database. So let’s start.

Step: 1 Hope you install Laravel 6 Application if not use this command 

laravel new laratinker

Step: 2 Create a Model and migration file for User

First, we create a Migration and Model file for the user’s table where we enter some data using Laravel Tinker.

php artisan make:model User -m

After that add some rows for the user migration file.

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Step: 2 Migrate User Table

Now, migrate Users’ migration file using this command.

php artisan migrate

Step: 3 Open Tinker

After migration user files open Laravel Tinker using this command.

php artisan tinker

After Run this command

We have A mode for User.


>>> $user = new App\User
=> App\User {#3024}
>>> $user->name = 'CoderMen'
=> "CoderMen"
>>> $user->email = '[email protected]'
=> "[email protected]"
>>> $user->password = bcrypt('password')
=> "$2y$10$GJCQzj71L56TDAG2wa3VNeSONgMWp8zAk2TQ1kfHVxPgARXg75fxO"
>>> $user->save()
=> true
>>>    

Here I simply Add an entry for the user using Laravel Tinker, Name, Email and Bcrypt password.

So here we complete tutorial on How To Use Tinker To Store Data in Database In Laravel 6 hope it helps you.