Understanding MVC in Laravel 12: Models, Views, and Controllers Explained
Laravel is one of the most popular PHP frameworks, and at its core lies the MVC architecture—a design pattern that helps keep your code clean, organized, and scalable.
But if you’re just getting started, the terms Model, View, and Controller can sound confusing.
In this blog, I’ll help you clearly understand what MVC is in Laravel with real-world analogies, simple code examples, and step-by-step explanation.
🎯 What is MVC?
MVC stands for:
| Part | Responsibility |
|---|---|
| Model | Handles data and business logic (Database) |
| View | Handles what the user sees (Frontend) |
| Controller | Acts as the middleman between Model and View |
Think of it like a restaurant:
- Model = Kitchen (prepares the data/food)
- View = Plate (presentation to the user)
- Controller = Waiter (gets order from user, asks kitchen, serves food)
🏗️ MVC in Laravel
Let’s break it down using a real Laravel example: A Blog where users can see a list of posts.
🔹 The Model (M)
In Laravel, Models are used to interact with the database.
Laravel provides Eloquent ORM (Object-Relational Mapping), which lets you work with your DB tables as PHP objects.
Example:
php artisan make:model Post
This creates a file at: app/Models/Post.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
// Laravel automatically maps this to 'posts' table
}
You can now use Post::all(), Post::create(), Post::find(), etc., to interact with the database.
🔹 The Controller (C)
Controllers handle the logic. They accept the request, interact with models, and return a response (view or data).
Example:
php artisan make:controller PostController
This creates: app/Http/Controllers/PostController.php
use App\Models\Post;
class PostController extends Controller
{
public function index()
{
$posts = Post::all(); // Fetch all posts from DB
return view('posts.index', compact('posts')); // Pass data to view
}
}
🔹 The View (V)
Views are Blade template files that display HTML. They receive data from the controller and render it beautifully.
Example: resources/views/posts/index.blade.php
<!DOCTYPE html>
<html>
<head>
<title>All Blog Posts</title>
</head>
<body>
<h1>Blog Posts</h1>
<ul>
@foreach ($posts as $post)
<li>{{ $post->title }}</li>
@endforeach
</ul>
</body>
</html>
🔁 How It All Connects
Here’s what happens when a user visits /posts:
- Laravel’s routes/web.php file receives the URL:
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
- The
PostController@indexmethod is called. - It fetches data from the Post model using
Post::all(). - It returns a view with the data.
- The view displays the data in HTML.
👨💻 Boom! That’s MVC in action.
🔍 Why Use MVC?
| Benefit | Description |
|---|---|
| 💡 Separation of concerns | Keep logic, UI, and data separate |
| ♻️ Reusable code | Reuse models and controllers |
| 📈 Scalable structure | Easily add new features |
| 🧹 Clean & maintainable | Easier to debug and update |
🛠 Real-World Example Summary
| Component | Code |
|---|---|
| Route | Route::get('/posts', [PostController::class, 'index']); |
| Controller | PostController@index fetches data and returns a view |
| Model | Post::all() fetches all blog posts |
| View (Blade) | Displays the posts using a loop |
📌 Final Thoughts
MVC is a powerful design pattern and Laravel uses it beautifully to build modern, robust web applications.
Once you understand how Models, Views, and Controllers work together, Laravel becomes much easier—and even fun—to work with!
Projects related to this guide
Use the concepts from this article in practical source-code projects.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure...
Python Flask Blog Project with Admin Panel
Are you ready to launch a powerful, modern, and fully customizable blog? Look no further! Introducing the Flask-...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes pr...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.