Kritim Yantra
May 20, 2025
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.
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:
Let’s break it down using a real Laravel example: A Blog where users can see a list of posts.
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.
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.
Controllers handle the logic. They accept the request, interact with models, and return a response (view or data).
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
}
}
Views are Blade template files that display HTML. They receive data from the controller and render it beautifully.
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>
Here’s what happens when a user visits /posts
:
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
PostController@index
method is called.Post::all()
.👨💻 Boom! That’s MVC in action.
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 |
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 |
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!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google