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

Read, Update, and Delete data through Model.

Read Data from Database through Model

First, we have to connect our model and database in our controller. For that we use below code in our controller:-

use DB;
use app/Model_Name;

After configuring DB and model, we make a function in which we create an instance of our model. After that, we use all() for fetching our data from the database. Before running the code we have to set Route for it. After setting the route let’s See the below code:-

Laravel model code using all() and foreach to print product name and description

See the output below:-

Browser output listing product names and descriptions fetched through the model

If we want to fetch a particular row then we use the first() method. See the below code:-

Laravel model code fetching the product with id 4 using where() and first()

in the above code, we provided id no. 4 so that it fetched all data of ID => 4. and if we can’t add where condition then it by default prints the first row. See the output below:-

JSON output of product id 4 with name Default_Name, quantity 12 and timestamps

Update Data from Database through Model

There is no method to update data, so to update data first we fetch the table and then declare the key-value and save it. See below:-

Laravel model code fetching id 4, setting name to Aman Kumar and calling save()

See the output below:-

PhpMyAdmin products table before and after update, id 4 name changed to Aman Kumar

Delete Data from Database through Model

We use delete() method to delete the data from the database through the model.

Laravel selectmodel function fetching the product with id 4 and calling delete() on it

See the output below:-

PhpMyAdmin product table before and after delete, with the row for id 4 removed

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

How to merge two or multiple tables to each other in the Laravel PHP Framework? (Part-2)

How to merge two or multiple tables to each other in the Laravel PHP Framework? Part-1 Part-3 Step 1. Go to app/Country.php. Now, we need to list…

Read More

CRUD Operation Using Model in Laravel.

Inserting data in Database through Model. (Click to Open) Read, Update, and Delete data through Model. (Click to Open) Sushant Kumar With MotoShare.in, you can book a…

Read More

Inserting data in Database through Model.

There are many methods for inserting data in database through Model. See below:- Migration is common in all methods in which we defined the table name and…

Read More

How to Update and Delete Data From Database table using Tinker.

How to Update Data in the Database table. We use the update command to update the data in the database table. Syntax Suppose, I want to update…

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

What is Laravel and use cases of Laravel?

What is Laravel? Laravel is an open-source, free PHP web framework designed for the development of web applications following the model-view-controller (MVC) architectural pattern. It was generated…

Read More