Laravel MVC Architecture: A Beginner's Guide with Simple Examples

Published on November 02, 2025
Laravel MVC PHP WebDevelopment InterviewPrep Architecture

What is MVC Architecture?

MVC stands for Model-View-Controller - a design pattern that separates an application into three main components. This separation makes your code more organized, maintainable, and scalable.

Think of MVC like a restaurant:

  • Model: The kitchen (handles data and business logic)
  • View: The dining area (presents information to customers)
  • Controller: The waiter (takes orders and coordinates between kitchen and dining area)

The Three Components of MVC in Laravel

1. Model (The Data Handler)

Models represent your application's data and business logic. They interact with the database.

// app/Models/User.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    // This model automatically links to the 'users' table
    
    protected $fillable = ['name', 'email', 'password'];
    
    // Business logic example
    public function isAdmin()
    {
        return $this->role === 'admin';
    }
}

What Models Do:

  • Communicate with the database
  • Define relationships between data
  • Contain business logic
  • Handle data validation rules

2. View (The Presentation Layer)

Views are what users see - the HTML templates. Laravel uses Blade templating engine.

// resources/views/users/profile.blade.php
<html>
<head>
    <title>User Profile</title>
</head>
<body>
    <h1>Welcome, {{ $user->name }}!</h1>
    <p>Email: {{ $user->email }}</p>
    
    @if($user->isAdmin())
        <div class="admin-badge">Administrator</div>
    @endif
    
    <ul>
    @foreach($user->posts as $post)
        <li>{{ $post->title }}</li>
    @endforeach
    </ul>
</body>
</html>

What Views Do:

  • Display data to users
  • Handle presentation logic
  • Use Blade templates for dynamic content
  • Remain clean without complex PHP logic

3. Controller (The Traffic Cop)

Controllers handle user requests, process data using Models, and return Views.

// app/Http/Controllers/UserController.php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function showProfile($userId)
    {
        // Get data from Model
        $user = User::findOrFail($userId);
        
        // Return View with data
        return view('users.profile', ['user' => $user]);
    }
    
    public function updateProfile(Request $request, $userId)
    {
        $user = User::findOrFail($userId);
        
        // Process data from request
        $user->update([
            'name' => $request->input('name'),
            'email' => $request->input('email')
        ]);
        
        return redirect()->route('user.profile', $userId);
    }
}

How MVC Components Work Together

Let's trace a complete user request:

Step-by-Step Flow:

  1. User visits /profile/1
  2. Route directs to appropriate controller:
    // routes/web.php
    Route::get('/profile/{id}', [UserController::class, 'showProfile']);
  3. Controller handles the request:
    public function showProfile($id)
    {
        $user = User::find($id);      // Model fetches data
        return view('profile', ['user' => $user]); // View displays data
    }
  4. Model interacts with database:
    // Behind the scenes, this runs: SELECT * FROM users WHERE id = 1
  5. View renders the final HTML for the user.

Real-World Example: Blog System

Let's see MVC in action with a blog:

Model (Post.php)

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    
    public function getExcerpt()
    {
        return Str::limit($this->content, 100);
    }
}

Controller (PostController.php)

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::with('user')->latest()->get();
        return view('posts.index', compact('posts'));
    }
    
    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }
}

View (posts/index.blade.php)

@foreach($posts as $post)
    <article>
        <h2>{{ $post->title }}</h2>
        <p>By: {{ $post->user->name }}</p>
        <p>{{ $post->getExcerpt() }}</p>
        <a href="/posts/{{ $post->id }}">Read More</a>
    </article>
@endforeach

Benefits of MVC Architecture

Separation of Concerns

  • Models handle data logic
  • Views handle presentation
  • Controllers handle application flow

Easier Maintenance

  • Fix issues in one component without breaking others
  • Multiple developers can work simultaneously

Better Testing

// Test controller logic
public function test_user_can_view_profile()
{
    $user = User::factory()->create();
    $response = $this->get("/profile/{$user->id}");
    $response->assertStatus(200);
}

Code Reusability

  • Use same models across multiple controllers
  • Reuse views with different data

Common Interview Questions & Answers

1. What is MVC and why is it important?
MVC separates application into Model, View, and Controller components, making code more organized, maintainable, and testable.

2. How does Laravel implement MVC?
Laravel uses Eloquent for Models, Blade for Views, and Controller classes to handle the application logic and coordination.

3. What goes in a Model vs Controller?
Models handle data logic and database interactions. Controllers handle HTTP requests, process data using models, and return responses.

4. Can you explain the request lifecycle in Laravel MVC?
Request → Routes → Controller → Model (database) → Controller → View → Response

5. What are Laravel's conventions for MVC?
Models: app/Models/User.php
Views: resources/views/users/
Controllers: app/Http/Controllers/UserController.php

Best Practices

  • Keep controllers thin: Move business logic to models or service classes
  • Views should be dumb: Avoid complex PHP logic in templates
  • Use meaningful names: UserController not Controller1
  • Follow Laravel conventions: They make your code predictable

Quick MVC Cheat Sheet

Component Responsibility Location
Model Data & Business Logic app/Models/
View Presentation & UI resources/views/
Controller Request Handling app/Http/Controllers/

Now you understand the foundation of Laravel! In our next post, we'll dive into Laravel Routing: All You Need to Know About GET, POST, and Resource Routes where you'll learn how to direct traffic in your application.