β
Model (M) – Handles database logic and interacts with tables.
β
View (V) – Manages frontend/UI and displays data.
β
Controller (C) – Acts as a bridge between Model & View, handling requests and responses.
π Example: User authentication in MVC
Models are stored in the app/Models/
directory.
π Example: Creating a User
model
php artisan make:model User
This command creates app/Models/User.php
, which interacts with the database.
Controllers handle HTTP requests and process logic.
π Example: Creating a UserController
php artisan make:controller UserController
This creates app/Http/Controllers/UserController.php
, where you can define logic.
π Example: Fetching users from the database
use App\Models\User;
class UserController extends Controller {
public function index() {
$users = User::all(); // Fetch all users
return view('users.index', compact('users'));
}
}
Views are stored in resources/views/
.
π Example: Displaying users in users/index.blade.php
@foreach($users as $user)
<p>{{ $user->name }}</p>
@endforeach
MVC in Laravel separates concerns and makes development structured and efficient.
π― Next Steps: Learn more about Routes, Middleware, and Eloquent ORM! π