What is Laravel?

Laravel is the most popular open-source web application framework for PHP. Designed around developer ergonomics and elegance, Laravel offers expressive routing, migrations, a powerful database ORM (Eloquent), and seamless authentication scaffolding right out of the box.


The Model-View-Controller (MVC) Pattern

Laravel organizes code using the architectural design pattern known as Model-View-Controller (MVC):

text
Browser Request  -->  Routing (routes/web.php)
                             |
                             v
                        Controller
                       /          \
                      v            v
             Model (Eloquent)    View (Blade)
                    |                  |
                    v                  v
                 Database           HTML UI
  1. Model (Data & Business Logic): Represents data entities (e.g. Question, User, Certificate) and defines database relationships.
  2. View (Presentation Layer): HTML templates with Blade directives for displaying information to users.
  3. Controller (Orchestrator): Accepts the HTTP request, queries the Model, and returns either a Blade View or JSON API response.

1. Defining Routes & Controllers

Routes connect URLs to controller actions:

php
// routes/web.php
use App\Http\Controllers\QuizController;

Route::get('/quizzes/{slug}', [QuizController::class, 'show'])->name('quiz.show');

And the corresponding controller:

php
// app/Http/Controllers/QuizController.php
namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\View\View;

class QuizController extends Controller {
    public function show(string $slug): View {
        $category = Category::where('slug', $slug)->firstOrFail();
        return view('quiz.show', ['category' => $category]);
    }
}

2. Eloquent ORM: Intuitive Database Queries

Eloquent models correspond to database tables. You can query records without writing raw SQL strings:

php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Category extends Model {
    protected $fillable = ['name', 'slug', 'is_enabled'];

    public function questions(): HasMany {
        return $this->hasMany(Question::class);
    }
}

// Eloquent usage:
$activeCategories = Category::where('is_enabled', true)
    ->withCount('questions')
    ->orderBy('name')
    ->get();

Frequently Asked Questions

What is the purpose of database migrations in Laravel?

Migrations act as version control for your database schema. They allow development teams to define, modify, and synchronize database table structures reproducibly across environments without manual SQL scripts.

What is middleware?

Middleware provides a convenient mechanism for inspecting and filtering HTTP requests entering your application (e.g. verifying authentication, CSRF tokens, or rate limits).


Continue Learning