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:-
- What is On-Page Optimization and Off-page Optimization - March 14, 2024
- [SOLVED] Flutter : PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null) - December 7, 2021
- [Solved] Flutter : Error: The getter ‘subhead’ isn’t defined for the class ‘TextTheme’ from package:flutter/src/material/text_theme.dart’ – searchable_dropdown - December 6, 2021