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

See the output below:-

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

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

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

See the output below:-

Delete Data from Database through Model

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

See the output below:-