CHAPTER 03
Beginner
Understanding MVC Architecture in Laravel
Updated: May 14, 2026
15 min read
# CHAPTER 3
Understanding MVC Architecture in Laravel
1. Introduction
When you open a new Laravel project, you are greeted by dozens of folders. It can be terrifying. Where do you write your database queries? Where does the HTML go? Laravel relies heavily on an architectural pattern called MVC (Model-View-Controller). In this chapter, we will demystify this pattern and map exactly how an HTTP request flows through the Laravel ecosystem.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the Model, View, and Controller layers.
- Trace the complete Request Lifecycle of a Laravel application.
- Understand Separation of Concerns.
- Locate the critical MVC directories within a Laravel project.
3. Beginner-Friendly Explanation
Imagine a luxury restaurant.- The Router (The Hostess): You walk in the door and say, "I have a reservation for John." The Hostess (Router) checks her list and says, "Ah yes, Table 5. I will send your Waiter."
- The Controller (The Waiter): The Waiter comes to your table. You request a steak. The Waiter does not cook the steak; they simply manage the workflow. They walk into the kitchen and hand the order to the Chef.
- The Model (The Chef): The Chef receives the order. The Chef goes to the massive pantry (the Database), grabs the meat, and cooks it perfectly. The Chef hands the cooked food back to the Waiter.
- The View (The Plating Station): The Waiter takes the food to a plating station where it is garnished and made to look beautiful (HTML/CSS). The beautifully plated meal is finally served to you.
4. Separation of Concerns
The entire point of MVC is that everyone has only one job.- If the HTML looks ugly, you don't bother the Chef (Model). You fix the Plating (View).
- If the Database query is slow, you don't bother the Waiter (Controller). You fix the Chef (Model).
5. The Model (Data)
The Model represents a specific table in your database. If you have ausers table in MySQL, you will have a User.php Model in Laravel.
-
Location:
app/Models/
- Job: Connects to the database, fetches data, inserts data, and holds business logic (like calculating a user's total age). It NEVER contains HTML.
6. The View (Presentation)
The View is what the user actually sees on their screen.-
Location:
resources/views/
- Job: Contains the HTML, CSS, and Laravel's special Blade templating code. It takes raw data handed to it by the Controller and formats it beautifully. It NEVER connects directly to the database.
7. The Controller (Logic)
The Controller is the middleman.-
Location:
app/Http/Controllers/
- Job: Receives the URL request, asks the Model for data, and passes that data to the View.
php
8. The Laravel Request Lifecycle
Here is the exact path a request takes when a user typeswww.yoursite.com/profile into their browser:
-
1.
public/index.php: The entry point. It boots up the entire Laravel framework.
-
2.
routes/web.php: The Router (Hostess) sees the/profileURL and points it to theUserController.
-
3.
UserController: The Controller (Waiter) asks theUserModel to fetch profile data.
-
4.
UserModel: The Model (Chef) runs a SQL query on the MySQL database and returns the data to the Controller.
-
5.
profile.blade.php: The Controller hands the data to the View (Plating). The View generates standard HTML.
- 6. Response: Laravel sends the beautiful HTML back to the user's browser.
9. Best Practices
- Fat Models, Skinny Controllers: This is an industry standard. Your Controller should only be a few lines long (Route traffic, ask for data, return View). If you have 50 lines of complex mathematical logic, that code belongs in the Model.
10. Common Mistakes
-
Writing Database Queries in the View: A beginner might open a View file (
profile.blade.php) and try to write$users = User::all()inside the HTML. This completely destroys the MVC pattern. Views are "dumb"; they should only display data handed to them by the Controller.
11. Exercises
- 1. Match the MVC component to its job:
- A) Interacts with MySQL.
- B) Holds HTML tags.
- C) Acts as the middleman routing data.
12. Coding Challenges
-
Challenge: Open a fresh Laravel project. Locate the
routes/web.phpfile, theapp/Http/Controllers/directory, and theresources/views/directory. Memorize these three locations; you will spend 90% of your career in them.
13. MCQs with Answers
Question 1
In Laravel's MVC architecture, which directory is responsible for holding the files containing HTML and CSS?
Question 2
What is the primary role of the Controller in MVC?
14. Interview Questions
- Q: Walk me through the Laravel Request Lifecycle. What happens from the moment a user hits Enter in their browser to the moment the HTML is rendered on their screen?
- Q: Explain the concept of "Separation of Concerns" within the context of MVC. Why is it frowned upon to write database queries directly inside a View file?