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

How to Insert and Read data from database using Controller.

First, make a Controller and View. To use the database query in the controller, we have to use database class (use DB;) in the controller. See the below image:-

FirstController.php in Laravel with the use DB; statement importing the database class

Insert data into Database.

First, make a function in the controller in which define a variable which is equal to the insertion Syntax and echo the variable.

Syntax

Where, R1 = Row1
R2 = Row2

See the below image for insertion of data into the database.

Laravel controller insert function using DB::table('tinker')->insert to add two rows of name and email

Now route the controller, start Laravel development server and open the page. As the page opens, the data inserted into the database and returns 1 as output. Now let’s check that our data is inserted or not.

PhpMyAdmin tinker table showing inserted rows for Sushant Kumar and Ajay Kumar with emails

Yeah, it’s inserted.

Read data from Database.

To read the data from a database we use get(). See the syntax below

$Variable= DB::table(“Table_name“)->get();

See the below image for help:-

Laravel controller code reading all rows with DB::table('tinker')->get() and print_r

See the output when we open the page. It shows all data inserted in the database.

Browser output of a Laravel Collection object listing the two tinker table records

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

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

Elastic Tutorial: Elasticsearch 8.X Installation & CRUD in Linux

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Python Tutorials: Files Operations

Python File Operations Python – Working with files Python File Operations: Modes Python File Operations: Writing a file Python File Operations: CRUD Rajesh Kumar I’m Rajesh Kumar,…

Read More

AJAX Crud with laravel 5.8

sam MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in advantage…

Read More

Elasticsearch Lab#1 – CRUD

Reference Sample Data & Query – https://github.com/devopsschool-demo-labs-projects/elasticsearch Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world…

Read More

CRUD USING API IN Flutter (Part 1)

INTRODUCTION: In general, the use of JSON is used to communicate data from client to server and from server to client. In this article we are going…

Read More