MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Terminal running php artisan tinker to open the shell, then echo Hello World printing its output

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

Laravel migration up method creating a tinker table with name, email, Location and created_at defaults

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.

Tinker shell running DB::table insert with name, email, location and created_at, returning true

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

PhpMyAdmin tinker table showing the new row for Sushant Kumar added through Tinker

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

Tinker insert command with only the name value Ajay Kumar, returning true

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

PhpMyAdmin row for Ajay Kumar where email and Location were filled with default values

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

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

Laravel syntax DB::table('Table_name')->insert with an array of key-value arrays, one per row

See the below Program to understands better:-

Php artisan tinker running DB::table('tinker')->insert for 3rd, 4th and 5th rows, returning true

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

PhpMyAdmin tinker table showing the newly inserted 3rd row, 4th row and 5th row records highlighted

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.

Related Posts

CRUD Operation Using Tinker in Laravel.

How to Insert Data in Database using Tinker in Laravel. (Click to Open) How to Read Data from Database in Tinker. (Click to Open) How to Update…

Read More

How to implement CRUD functions in the Laravel PHP Framework? Part-1

How to implement CRUD functions in the Laravel PHP Framework? Within computer programming, the acronym CRUD stands for create, read, update and delete. Step 1. Create a…

Read More

Todo Application in Laravel

1.Create Project in Laravel Composer create-project laravel/laravel todolist 2.Create CRUD Controller php artisan make:controller TodosController –resource 3.Route setup of CRUD Controller routes/web.php 4.Create Database todolist localhost/phpmyadmin create…

Read More

Python Tutorials: Database operations using python – mysql

This tutorial connects Python to MySQL with the official mysql-connector-python driver and runs the full set of CRUD operations: create a database and table, then insert, read,…

Read More

Laravel: What is APP_KEY in Laravel?

Let’s break down both what APP_KEY is in a Laravel .env file, why it matters, and its use cases. I’ll also explain how it’s generated and what…

Read More

artisan Tutorials: php artisan passport

When you run php artisan passport:install, Laravel attempts to do the following: Running php artisan passport:install is a crucial setup step when using Laravel Passport for API…

Read More