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.
MVC architecture in 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.
<?php // Model example namespace App\Models; use Illuminate\Database\Eloquent\Model; class Product extends Model { // Laravel assumes 'products' table }
<?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]); } }
<!-- View example: resources/views/products/index.blade.php -->
<ul>
@foreach ($products as $product)
<li>{{ $product->name }}</li>
@endforeach
</ul>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.
<?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>
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.
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.
Practice
Solution
Step 1: Understand MVC roles
Model handles data, View handles UI, Controller handles logic and user input.Step 2: Identify the component for logic
Controller processes requests, applies logic, and returns responses.Final Answer:
Controller -> Option AQuick Check:
Logic and input handling = Controller [OK]
- Confusing Model with Controller
- Thinking View handles logic
- Assuming Route handles logic
show method of UserController in Laravel?Solution
Step 1: Recall Laravel route syntax
Routes useRoute::get('uri', 'Controller@method')format for controller methods.Step 2: Match correct syntax
Route::get('/user/{id}', 'UserController@show'); matches this pattern exactly; others have syntax errors or wrong order.Final Answer:
Route::get('/user/{id}', 'UserController@show'); -> Option DQuick Check:
Correct route syntax = Route::get('/user/{id}', 'UserController@show'); [OK]
- Using array without quotes
- Swapping controller and method order
- Using anonymous function incorrectly
/welcome route?public function welcome() {
$name = 'Alice';
return view('greeting', ['name' => $name]);
}Assuming the
greeting.blade.php view contains:<h1>Hello, {{ $name }}!</h1>Solution
Step 1: Understand data passing to view
The controller passes ['name' => 'Alice'] to the view as variable $name.Step 2: Interpret Blade template output
The view uses {{ $name }} which outputs 'Alice' safely inside <h1> tags.Final Answer:
<h1>Hello, Alice!</h1> -> Option BQuick Check:
Passed variable rendered correctly = <h1>Hello, Alice!</h1> [OK]
- Expecting raw variable name output
- Confusing PHP variable syntax inside Blade
- Assuming missing variable error
public function index() {
$users = User::all;
return view('users.index', compact('users'));
}Solution
Step 1: Check method call syntax
User::all is a method and requires parentheses: User::all() to fetch all users.Step 2: Verify other parts
View name and compact usage are correct; import is assumed correct or handled by namespace.Final Answer:
Missing parentheses after all() method -> Option AQuick Check:
Method calls need () = Missing parentheses after all() method [OK]
- Forgetting parentheses on method calls
- Misusing compact() function
- Assuming view name syntax error
Solution
Step 1: Recall MVC responsibilities
Model handles data, Controller handles logic and data fetching, View handles display.Step 2: Evaluate options
Fetch products and categories in Controller, pass to View, and display there correctly fetches data in Controller and passes it to View for display, following MVC.Final Answer:
Fetch products and categories in Controller, pass to View, and display there -> Option CQuick Check:
Controller fetches, View displays = Fetch products and categories in Controller, pass to View, and display there [OK]
- Putting SQL in Views
- Mixing data logic in Models with display
- Letting Views fetch data
Ready to prove it?
Pass the test and this pattern moves unknown
