Laravel Blade is a powerful templating engine that provides an elegant syntax while compiling to pure PHP. This guide covers Laravel 12's Blade features and best practices.
// Basic view rendering Route::get('/', function () { return view('welcome', ['name' => 'Samantha']); }); // With route model binding Route::get('/users/{user}', function (User $user) { return view('users.profile', compact('user')); });
{{-- Safe, escaped output --}} <h1>Hello, {{ $name }}</h1> {{-- Raw, unescaped HTML (use with caution) --}} <div>{!! $htmlContent !!}</div> {{-- PHP blocks --}} @php $counter = 1; $items = collect(['one', 'two', 'three']); @endphp
{{-- If Statements --}} @if ($user->isAdmin()) <admin-badge/> @elseif ($user->isModerator()) <mod-badge/> @else <user-badge/> @endif {{-- Unless Statement --}} @unless (Auth::check()) <p>Please log in</p> @endunless {{-- New in Laravel 12: match statements --}} @match($role) @case('admin') <admin-panel/> @break @case('user') <user-dashboard/> @break @default <guest-view/> @endmatch
@auth('admin') <admin-dashboard/> @elseguest <login-form/> @endauth @guest <p>Welcome, please login</p> @endguest
@production <!-- Production Analytics --> <script src="analytics.js"></script> @endproduction @env(['local', 'staging']) <dev-toolbar/> @endenv
@foreach ($users as $user) <user-card :user="{{ $user }}"/> @empty <p>No users found.</p> @endforeach {{-- Loop with index --}} @for ($i = 0; $i < count($items); $i++) <span>Item {{ $i + 1 }}: {{ $items[$i] }}</span> @endfor
@foreach ($users as $user) {{-- Available properties --}} {{ $loop->index }} {{-- 0 based --}} {{ $loop->iteration }} {{-- 1 based --}} {{ $loop->remaining }} {{ $loop->count }} {{ $loop->first }} {{ $loop->last }} {{ $loop->even }} {{ $loop->odd }} {{ $loop->depth }} {{-- Nested Loops --}} @foreach ($user->posts as $post) @if ($loop->parent->first) <span>First user's post</span> @endif @endforeach @endforeach
{{-- Dynamic Classes --}} <div @class([ 'base-class', 'active' => $isActive, 'disabled' => $isDisabled, 'error' => $errors->has('field') ])> Content </div>
{{-- resources/views/components/alert.blade.php --}} <x-alert type="error" :message="$message"/> {{-- With slots --}} <x-card> <x-slot:title> Dashboard </x-slot> <x-slot:content> Main content here </x-slot> </x-card>
{{-- Inline styles --}} <x-style> .custom-element { background: theme('colors.primary'); } </x-style> {{-- Inline scripts --}} <x-script> function initializeComponent() { // Component logic } </x-script>
{{-- Prefer this --}} <x-user-profile :user="$user"/> {{-- Over this --}} @include('partials.user-profile', ['user' => $user])
class UserProfileViewModel { public function __construct(public User $user) {} public function fullName(): string { return "{$this->user->first_name} {$this->user->last_name}"; } }
{{-- AppServiceProvider.php --}} Blade::directive('datetime', function ($expression) { return "<?php echo Carbon\Carbon::parse($expression)->format('Y-m-d H:i'); ?>"; }); {{-- In template --}} <span>Posted: @datetime($post->created_at)</span>
{{ }}
@csrf
in formsFor more information, visit the Laravel 12 Documentation.