Getting Started with Laravel 12: A Complete Guide
Laravel has long been one of the most popular PHP frameworks, known for its elegant syntax, robust features, and developer-friendly tooling. With Laravel 12, developers can enjoy even more improvements for building modern web applications quickly and efficiently.
Laravel has long been one of the most popular PHP frameworks, known for its elegant syntax, robust features, and developer-friendly tooling. With Laravel 12, developers can enjoy even more improvements for building modern web applications quickly and efficiently.
This guide is designed to help beginners and intermediate developers get started with Laravel 12, covering installation, basic concepts, routing, controllers, views, and best practices.
Why Laravel 12?
Laravel continues to evolve, and version 12 brings new features and optimizations, including:
- Enhanced route handling and speed improvements
- Updated authentication scaffolding
- Better integration with modern front-end tools
- Cleaner syntax and improved developer experience
Whether you’re building a small project or a large-scale application, Laravel 12 provides a solid foundation.
1. Installing Laravel 12
You can create a new Laravel project via Composer:
composer create-project laravel/laravel:^12.0 my-laravel-app cd my-laravel-app php artisan serve
Your application will run on http://localhost:8000 by default.
2. Understanding the Directory Structure
Laravel 12 organizes your application cleanly:
- app/ – contains core application logic, models, controllers
- routes/ – define web and API routes (web.php, api.php)
- resources/ – views, CSS, JS, and Blade templates
- database/ – migrations, seeds, and factories
- config/ – application configuration
This structure makes it easy to scale projects while keeping code organized.
3. Routing Basics
Routes in Laravel define how URLs map to controllers or closures.
Example in routes/web.php:
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about');
});
For dynamic routes:
Route::get('/user/{id}', function ($id) {
return "User ID: " . $id;
});
4. Controllers
Controllers handle application logic and help keep routes clean.
Generate a controller:
php artisan make:controller UserController
Example controller:
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
return view('users.index', ['users' => User::all()]);
}
public function show($id)
{
return view('users.show', ['user' => User::find($id)]);
}
}
Then connect the routes:
Route::get('/users', [UserController::class, 'index']);
Route::get('/users/{id}', [UserController::class, 'show']);
5. Blade Templates
Laravel’s Blade templating engine allows you to write clean, reusable HTML templates.
Example layout: resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html>
<head>
<title>My Laravel App</title>
</head>
<body>
<header>
<h1>Welcome to Laravel 12</h1>
</header>
<main>
@yield('content')
</main>
</body>
</html>
Child view: resources/views/users/index.blade.php
@extends('layouts.app')
@section('content')
<h2>Users List</h2>
<ul>
@foreach ($users as $user)
<li>{{ $user->name }} ({{ $user->email }})</li>
@endforeach
</ul>
@endsection
6. Database and Eloquent ORM
Laravel 12 uses Eloquent ORM for database interactions.
- Define a model: php artisan make:model User
- Create a migration: php artisan make:migration create_users_table
- Run migrations: php artisan migrate
Eloquent allows you to query data elegantly:
$users = User::where('active', 1)->get();
7. Laravel Artisan Commands
Artisan is Laravel’s command-line tool:
- php artisan serve – run development server
- php artisan make:controller NameController – create controller
- php artisan make:model Name – create model
- php artisan migrate – run database migrations
- php artisan tinker – interact with application in REPL
8. Authentication
Laravel 12 offers built-in authentication scaffolding. Install with:
composer require laravel/breeze --dev php artisan breeze:install npm install && npm run dev php artisan migrate
You now have registration, login, and logout functionality out of the box.
9. Best Practices for Beginners
- Keep controllers slim; use services for complex logic
- Validate user input with Laravel validation
- Use environment variables for sensitive information
- Leverage Laravel collections for data manipulation
- Write tests using PHPUnit or Pest
Conclusion
Laravel 12 is a developer-friendly framework that makes building modern web applications efficient and enjoyable. With its clean syntax, Blade templates, Eloquent ORM, and powerful CLI tools, you can rapidly develop scalable, maintainable projects.
Getting started is easy, and once you master routing, controllers, views, and database interactions, you’ll be ready to tackle more advanced features like APIs, queues, and event-driven development.