Laravel applications follow the traditional Model-View-Controller(MVC) design pattern. This pattern helps you to tier your code for easier maintenance. By keeping the data-manipulating logic separate from the bits that handle the display, you make it much easier to change either the template or the underlying code without touching the other.
It helps to interact with your database and retrieve your objects’ information.It consists of a layer residing between the data and the application. The data itself can be stored in various types of database systems such as MySQL or even simple XML or Excel files.
Views are the visual representation of our web application, they are responsible for presenting the data that the Controller received from the Model. They can be easily built using the Blade template language that comes with Laravel or simply using plain PHP code. Blade is driven by template inheritance and sections. When Laravel renders these Views it examines first their file extension, and depending on it being either “.blade.php” or simply “.php”, determines if Laravel treats our View as a Blade template or not.
The function of a Controller is to handle requests and pass data from the Model to the Views. Thus a Controller can be considered as the link between our Model and Views.
Apart from this, routes are used to map URLs to designated controller actions, as shown in the figure below:
- A request is made when a user enters a URL associated with your application.
- A route associated with that URL maps the URL to a controller action.
- That controller action leverages the necessary model to retrieve information from the database and then passes that data off to a view.
- And that view renders the final page.
It is an object-relational mapper, It is named “Eloquent,” because it allows you to work with your database objects and relationships using an eloquent and expressive syntax. The Eloquent ORM is Laravel’s built-in ORM implementation. Every table in the database possess a corresponding Model through which the developer interact with said table.
The Laravel Schema class provides a database agnostic way of manipulating tables. It helps in managing all database related work such as creating or deleting tables and adding fields to an existing table. It works with a multitude of databases systems supported by Laravel and MySQL being the default one. The Schema class has the same API across all of these database systems.
3.Managing the database with Migrations
Migrations can be considered as a form of version control for our database. They allow us to change the database schema and describe and record all those specific changes in a migration file. Each Migration is usually associated with a Schema Builder to effort-lessly manage our application’s database. A migration can also be reverted or “rolled back” using the same said file.
By Using terminal we can issue the following commands to create or drop tables in our database:
Fig:Commands to create or drop tables in database
The Seeder class lets us seed data into our tables.All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.
When the backend is empty we can populate it with some data by simply issuing the following command in the terminal:
$ php artisan db:seed