Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Tinker Command and How to Insert Data in Database using Tinker in Laravel.

Tinker Command(php artisan tinker) is used to interact directly with the database table to insert, delete, update data. It executes SQL commands through the command line.

When we run the tinker command, it will open a shell where we can execute php commands. See below:-

How to use tinker in our project.

To use tinker, we have to make a migration in which we define a table and also defines the column. Make migration by using php artisan make:migration Name –create=Table_name command and define columns as I defined. See the below image:-

In the above example, I defined 4 columns of Name, E-mail, Location, and created_at, and also defined default values because if we can’t provide the value of any column then it assigns the default value in the database rather than giving error.

If we can’t provide a default value and adding the value of one column then, it gives an error that “This field is required” so we define a default value in each column to overcome from this type of error.

How to Insert Data in Database using Tinker.

To insert data in database table we use below PHP command. See below:-

Syntax

DB::table(“Table_Name”)->insert([“Column1″=>”Column1_data”, “Column2″=>”Column2_data”, “Column3″=>”Column3_data”);

For Example:- My table name is tinker and i’m inserting Name, E-mail, Location, and time indication created_at as mentioned above. See the below image what happened when i executed the command.

Let’s See in Database, my data added or not.

What happens when we insert value only for one column.

When we insert a value in only one column then, other columns filled by default value as I said above. See the below image for help:-

Let’s see in the Database, Other columns are filled with a Default value or not.

How to insert multiple Row’s in Database(Multiple rows Insertion).

To insert multiple rows in the database we use below syntax:-

See the below Program to understands better:-

Now, let’s see the database that our data was inserted or not.