model are used to define the tables of the db interm of classes so that we have more controll over the database functionality
we can
- change the table name
- combine the tables
- write logic and mathematical functions
- define database structure also
Model name will always be singular
suppose you are having a table named “employees” so the model will be “employee”
Now if you are having your table name as emp then can you map that in your model ?
The answer is YES you have to add a line in your model and you are good to go
Requirement
when you are using the model then you need a controller also that will help to deal with routes and connection between routes and model.
to make model
php artisan make:model User
now as we have discussed if you are a table called “users” then the User model will automatically map your model to the table
to make controller
php artisan make:controller UserController
UserController →
::all() is a method of model to fetch all the data
Configure controller on route → to see the result of model in your pc
Diffrent table name with diffrent name of model
suppose you are having a table that is completely diffrent name and then also you want to fetch the data of that table so you have to go to your model and add a line of code under class
public $table="table_name"; // and this will map the current
To save data
first make a controller and a model
take the controller’s page as post route and call the model
Inside model →
you have to redirect also after save() operation…
To update
make a route for “edit” route with id → for get request
without id for post request