Skip to main content
Laravel Basics Tutorial
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).
This structure allows massive teams of developers to work on the exact same app without stepping on each other's toes.

5. The Model (Data)

The Model represents a specific table in your database. If you have a users 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
123456789101112131415
<?php
namespace App\Http\Controllers;
use App\Models\User; // Import the Chef (Model)

class UserController extends Controller
{
    public function showProfile()
    {
        // 1. Waiter asks Chef for data
        $users = User::all(); 
        
        // 2. Waiter hands data to Plating Station (View)
        return view(&#039;profile', ['users' => $users]); 
    }
}

8. The Laravel Request Lifecycle

Here is the exact path a request takes when a user types www.yoursite.com/profile into their browser:
  1. 1. public/index.php: The entry point. It boots up the entire Laravel framework.
  1. 2. routes/web.php: The Router (Hostess) sees the /profile URL and points it to the UserController.
  1. 3. UserController: The Controller (Waiter) asks the User Model to fetch profile data.
  1. 4. User Model: The Model (Chef) runs a SQL query on the MySQL database and returns the data to the Controller.
  1. 5. profile.blade.php: The Controller hands the data to the View (Plating). The View generates standard HTML.
  1. 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. 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.php file, the app/Http/Controllers/ directory, and the resources/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?

15. FAQs

Q: Does Laravel force me to use MVC? A: Technically, no. You could write all your database queries directly in the routing file. However, as your app grows past 3 pages, doing so will create a horrific, unmaintainable mess. Sticking to MVC is highly recommended.

16. Summary

In Chapter 3, we mapped the blueprint of Laravel. The MVC architecture is a system of extreme organization. By strictly separating our database logic (Models), our traffic management (Controllers), and our HTML presentation (Views), we ensure our codebase remains clean, readable, and highly scalable. Understanding this flow is the key to unlocking the rest of the framework.

17. Next Chapter Recommendation

Before the Controller can do its job, the Router has to direct traffic to it. Proceed to Chapter 4: Laravel Routing Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·