PHP Laravel Introduction
Route::get("/path", [ControllerName::class, "method"]); // routes/web.php
@foreach ($items as $item) {{-- Blade template --}}
{{ $item->property }}
@endforeach
$model = ModelName::find($id); // Eloquent
What is Laravel?
Laravel is the most widely adopted PHP framework, built around the MVC pattern and known for prioritizing developer productivity through expressive syntax and a rich set of built-in tools.
Example: What is Laravel?
<?php
// Print "Laravel: MVC-based, developer-friendly PHP framework" to the output
echo "Laravel: MVC-based, developer-friendly PHP framework";
?>
Login to try C/C++/Java/PHP code in the editor
Basic Routing
Laravel's router maps incoming URLs to specific controller methods or inline closures in a single, readable routes file, keeping your application's URL structure easy to see at a glance.
Example: Basic Routing
<?php
// routes/web.php:
// Route::get('/users/{id}', [UserController::class, 'show']);
echo "Maps a URL pattern to a controller method";
?>
Login to try C/C++/Java/PHP code in the editor
Blade Templating
Blade, Laravel's templating engine, compiles familiar-looking template syntax (like @if and @foreach) down into plain, cached PHP, giving you clean templates without sacrificing performance.
Example: Blade Templating
<?php
// Blade template syntax (compiles to plain PHP):
// @if($isAdmin) <p>Admin</p> @endif
echo "@if and @foreach compile down to cached plain PHP";
?>
Login to try C/C++/Java/PHP code in the editor
Eloquent ORM
Eloquent is Laravel's ActiveRecord-style ORM, letting you query and manipulate database rows as PHP objects (like User::find(1)->save()) instead of writing raw SQL for common operations.
Example: Eloquent ORM
<?php
// $user = User::find(1);
// $user->name = "Alice";
// $user->save();
echo "Query and manipulate rows as PHP objects instead of raw SQL";
?>
Login to try C/C++/Java/PHP code in the editor
Artisan CLI Commands
Artisan is Laravel's command-line tool, used to scaffold new controllers, models, and migrations in seconds and to run maintenance tasks like clearing caches or running the test suite.
Example: Artisan CLI Commands
<?php
// Run in terminal: php artisan make:controller UserController
echo "Artisan scaffolds controllers, models, and migrations";
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting to run
composer installor copy.envafter cloning a project. - Using
{{ }}in Blade for HTML that should not be escaped, when{!! !!}prints raw output. - Forgetting the
webmiddleware or CSRF token on POST routes, so forms fail with a 419 error.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: