0
0
Laravelframework~5 mins

MVC architecture in Laravel

Choose your learning style9 modes available
Introduction

MVC helps organize your code by separating data, user interface, and control logic. Laravel uses MVC to keep your app clean and easy to manage.

Building a web app that needs clear separation between data and views.
Working on a team where different people handle data, design, and logic.
Creating apps that will grow and need easy maintenance.
Wanting to reuse code parts like views or data models.
Needing to test parts of your app separately.
Syntax
Laravel
Model: app/Models/Example.php
Controller: app/Http/Controllers/ExampleController.php
View: resources/views/example.blade.php

Route: routes/web.php

// Basic flow:
Route::get('/example', [ExampleController::class, 'index']);

class ExampleController extends Controller {
    public function index() {
        $data = Example::all();
        return view('example', ['data' => $data]);
    }
}

Models handle data and database.

Controllers handle user requests and app logic.

Views show the user interface using Blade templates.

Examples
This model represents the 'products' table in the database.
Laravel
<?php
// Model example
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Product extends Model {
    // Laravel assumes 'products' table
}
This controller fetches all products and sends them to a view.
Laravel
<?php
// Controller example
namespace App\Http\Controllers;
use App\Models\Product;

class ProductController extends Controller {
    public function showAll() {
        $products = Product::all();
        return view('products.index', ['products' => $products]);
    }
}
This view lists all product names using Blade syntax.
Laravel
<!-- View example: resources/views/products/index.blade.php -->
<ul>
@foreach ($products as $product)
    <li>{{ $product->name }}</li>
@endforeach
</ul>
Sample Program

This example shows a simple Laravel MVC setup. The route calls the TaskController's index method. The controller gets all tasks from the Task model and passes them to the view. The view lists all task titles.

Laravel
<?php
// routes/web.php
use App\Http\Controllers\TaskController;

Route::get('/tasks', [TaskController::class, 'index']);

// app/Models/Task.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Task extends Model {
    // Laravel uses 'tasks' table by default
}

// app/Http/Controllers/TaskController.php
namespace App\Http\Controllers;
use App\Models\Task;

class TaskController extends Controller {
    public function index() {
        $tasks = Task::all();
        return view('tasks.index', ['tasks' => $tasks]);
    }
}

// resources/views/tasks/index.blade.php
<ul>
@foreach ($tasks as $task)
    <li>{{ $task->title }}</li>
@endforeach
</ul>
OutputSuccess
Important Notes

Laravel automatically links models to database tables by naming convention.

Blade templates help keep HTML clean and safe.

Use routes to connect URLs to controller actions.

Summary

MVC splits your app into Model (data), View (UI), and Controller (logic).

Laravel follows MVC to keep code organized and easy to maintain.

Use routes to connect user requests to controllers, which use models and views.