Getting Started with Laravel

Laravel is the most popular PHP framework in the world. In this lesson you'll install it with Composer , understand its MVC structure, declare routes, write controllers, render Blade templates, model your data with Eloquent , and drive it all from the Artisan command line.

Learn Getting Started with Laravel in our free PHP course — an interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Installing Laravel with Composer

Laravel is distributed as a Composer package , so creating a new app is a single command. composer create-project laravel/laravel blog downloads the framework, installs all its dependencies, and scaffolds a complete project. From there, php artisan serve launches a local development server. Artisan — Laravel's command-line tool — is how you'll generate code, run migrations, and manage the app throughout the lesson.

After scaffolding, the project follows a fixed layout: routes live in routes/web.php , controllers in app/Http/Controllers , models in app/Models , and views in resources/views . Knowing where each kind of file belongs is half of learning a framework.

2️⃣ Routing: Mapping URLs to Actions

A route connects an HTTP method and URL to the code that should run. You declare them in routes/web.php with calls like Route::get , Route::post , and friends. An action can be an inline closure or — better for real logic — a controller method . Routes can take URL parameters like and be wrapped in middleware groups so, for example, every route in a group requires a logged-in user.

Run php artisan route:list any time to see every route your app answers and which action handles it — invaluable when a project grows large.

3️⃣ Controllers: The Logic Layer

A controller groups the actions for one resource — listing, showing, creating, updating. Generate one with php artisan make:controller PostController . Each public method handles a route: it fetches data (usually via Eloquent), maybe validates input, and returns a view or a redirect. Crucially, a controller should never trust raw input — call $request->validate() first so the rest of the method only runs on clean data.

Notice findOrFail() : it returns the record or automatically throws a 404 if the id doesn't exist, so you don't have to write that check by hand. This kind of sensible default is everywhere in Laravel.

4️⃣ Blade Templates: The View Layer

Blade is Laravel's templating engine. Files end in .blade.php and compile down to plain PHP. Blade adds clean directives — @if , @foreach , @extends for layout inheritance — and the syntax that echoes a value and escapes it , stopping XSS automatically. That makes views readable while keeping output safe by default.

The @extends / @section pair lets every page share one parent layout (header, nav, footer) and only fill in its own content — the templating equivalent of inheritance.

5️⃣ Eloquent ORM: The Model Layer

Eloquent is Laravel's object-relational mapper. Each model class maps to a database table, and table rows become objects you can read, change, and save — no hand-written SQL. You define the table's structure in a migration (run with php artisan migrate ), set $fillable for safe mass-assignment, and declare relationships like belongsTo and hasMany as methods so related records are just a property away.

Now you try the two core skills — declaring a route and validating input. Fill in each ___ using the 👉 hint, then run the matching artisan command and check the Output panel.

One more. A controller must validate before it saves. Add the validation call and the missing rule.

📋 Quick Reference — Laravel Basics

No code is filled in this time — just a brief and an outline. Build it yourself in a real Laravel project: generate the model and migration, wire up routes, list tasks in a Blade view, and add a validated create action. This is the full MVC loop you'll use in every Laravel app.

Practice quiz

Which command creates a brand-new Laravel project with Composer?

  • composer create-project laravel/laravel myapp
  • php artisan new myapp
  • laravel build myapp
  • composer require laravel

Answer: composer create-project laravel/laravel myapp. composer create-project laravel/laravel myapp downloads the framework and scaffolds a fresh application directory ready to run.

Which architectural pattern does Laravel follow?

  • Event sourcing
  • Pipes and filters
  • MVC (Model-View-Controller)
  • MVVM only

Answer: MVC (Model-View-Controller). Laravel is built around the Model-View-Controller pattern: models hold data, views render output, and controllers coordinate between them.

How do you register a route that answers a GET request for the home page?

  • app()->route('/')
  • Route::get('/', fn () => view('home'))
  • Http::get('/')
  • Router::add('/')

Answer: Route::get('/', fn () => view('home')). Route::get(uri, action) registers a GET route. The action can be a closure or a controller method.

What is Eloquent in Laravel?

  • A queue driver
  • A testing framework
  • A templating engine
  • Its object-relational mapper (ORM)

Answer: Its object-relational mapper (ORM). Eloquent is Laravel's ORM. Each model maps to a database table, and rows become objects you can query and save fluently.

Which Artisan command generates a new database migration?

  • php artisan make:migration create_posts_table
  • php artisan db:new posts
  • composer migrate posts
  • php artisan schema:add posts

Answer: php artisan make:migration create_posts_table. make:migration scaffolds a timestamped migration class where you define the schema in up() and down().

What templating engine does Laravel use for views?

  • Mustache
  • Blade
  • Twig
  • Smarty

Answer: Blade. Blade is Laravel's templating engine. It compiles .blade.php files to plain PHP and adds directives like @if, @foreach, and template inheritance.

Where do you define a one-to-many relationship between User and Post models?

  • In the migration only
  • In config/database.php
  • In the route file with Route::relation
  • As methods using hasMany() / belongsTo() on the Eloquent models

Answer: As methods using hasMany() / belongsTo() on the Eloquent models. Relationships are methods on the models: User has a posts() method returning $this->hasMany(Post::class), and Post has user() returning belongsTo(User::class).

What is the primary job of route middleware in Laravel?

  • To compile Blade templates
  • To seed the database
  • To filter or modify HTTP requests entering the application (e.g. auth checks)
  • To register service providers

Answer: To filter or modify HTTP requests entering the application (e.g. auth checks). Middleware wraps requests, running code before and after the controller. Common uses are authentication, CSRF protection, and rate limiting.

How do you validate incoming request data in a controller?

  • title
  • required|max:255

Answer: title. $request->validate([...]) checks the input against rules; on failure it automatically redirects back with errors, so the controller body only runs on valid data.

What is a Laravel facade such as Cache or DB?

  • A frontend CSS framework
  • A static-looking proxy that resolves a service out of the service container
  • A database table
  • A Blade directive

Answer: A static-looking proxy that resolves a service out of the service container. Facades give a clean, static-style interface (Cache::get(...)) to objects resolved from the service container, while still being backed by real, testable instances.