Blade Layouts, Loops, CSRF, and Components
Lesson 4 of 33 in the Laravel learning path.
How do I render a layout and a safe form in Blade?
Blade is Laravel's template language. A layout holds the document shell. A child view extends that layout and fills named sections. Curly braces escape HTML, which is the default protection against a lesson title that contains a script tag. The unescaped {!! !!} syntax is for trusted HTML you generated yourself, not for columns filled in by users.
Forms that change data need @csrf so the web middleware can match the token to the session. @method spoofs PUT or DELETE inside a normal POST, because HTML forms only send GET and POST. @forelse covers the empty catalog without a second if around the loop.
Syntax & Parameters
@extends('layouts.app') @section('content') @csrf @endsection
Example
Interactive preview available{{-- resources/views/layouts/app.blade.php --}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>@yield('title', 'Lessons')</title>
</head>
<body>
<main>@yield('content')</main>
</body>
</html>
{{-- resources/views/lessons/index.blade.php --}}
@extends('layouts.app')
@section('title', 'Lesson catalog')
@section('content')
<h1>Lesson catalog</h1>
@forelse($lessons as $lesson)
<article>
<h2><a href="{{ route('lessons.show', $lesson) }}">{{ $lesson->title }}</a></h2>
<p>{{ $lesson->track }}</p>
</article>
@empty
<p>No lessons have been published yet.</p>
@endforelse
@endsection
{{-- resources/views/lessons/create.blade.php --}}
@extends('layouts.app')
@section('content')
<x-alert />
<form method="post" action="{{ route('lessons.store') }}">
@csrf
<label for="title">Title</label>
<input id="title" name="title" value="{{ old('title') }}" required>
@error('title')<p>{{ $message }}</p>@enderror
<button type="submit">Save lesson</button>
</form>
@endsection
{{-- resources/views/components/alert.blade.php --}}
@if(session('status'))
<p role="status">{{ session('status') }}</p>
@endif
Expected Output
A catalog page with one article per lesson, or the sentence "No lessons have been published yet." The create form submits: _token = (session token) title = Intro to routes A title value of <b>Hi</b> is stored as text and displayed as the characters <b>Hi</b>, not as bold HTML.
Line-by-Line Walkthrough
- The layout yields a title and a content area. Child views fill those holes.
- @extends chooses the layout. @section names the hole being filled.
- @forelse prints each lesson and the @empty branch when the collection is empty.
- route('lessons.show', $lesson) uses the route name and the model's route key.
- {{ $lesson->title }} escapes HTML. A title of <script> renders as text.
- The create form posts to the named store route.
- @csrf inserts the hidden token input the VerifyCsrfToken middleware expects.
- old('title') repopulates the field after a failed validation redirect.
- @error prints the message for that attribute from the error bag.
- <x-alert> renders the component class-less Blade file in resources/views/components/alert.blade.php.
- The component reads the flash session key status, set by a redirect with ->with('status', 'Saved.').
- Nothing in this template trusts raw HTML from the database.
Common Pitfalls & Security
{!! $lesson->title !!} will execute markup stored in the title. Use it only for HTML you sanitized on purpose.
- A POST form without @csrf receives a 419 Page Expired response.
- @method('DELETE') is required for a delete button. A form method="delete" is ignored by browsers and arrives as GET.
- @foreach on an empty list renders nothing and looks like a broken page. @forelse gives you the empty copy.
- Echoing env('APP_KEY') or the CSRF token into JavaScript logs leaks secrets into the page source. The token belongs in the form field.
- Putting @php blocks of queries in the template skips the controller and is invisible to authorization.
- Components are not a security boundary. They still escape with {{ }} unless you mark a slot as HTML.
Best Practices & Modern Standards
Laravel 12 compiles Blade to cached PHP in storage/framework/views. Escape by default, keep queries in the controller, and pass a paginator or a small collection rather than every column of every row. Use @csrf on every form that uses the web middleware.
Class-based components are worth it when a badge needs PHP logic. A status flash is a single anonymous component, which is the file shown above. Run php artisan make:component Alert --view if you want the artisan stub.
Real-World Use
The course catalog is the layout plus the index section. After an instructor saves a lesson, the controller redirects back with a status flash and the alert component reads it. Student-written titles stay escaped, so a title cannot inject a script into the catalog page other students open.
Frequently Asked Questions
When is {!! !!} acceptable?
When the string is HTML your application built, such as a trusted markdown renderer with a sanitizer. Columns typed by users, including titles and comments, stay on {{ }}.
Why does my form return 419?
The CSRF token is missing or the session expired. Add @csrf inside the form and make sure the page is served by a route in the web middleware group.
What is the difference between @yield and @section?
The layout @yield names a slot. The child @section fills it. @stack and @push are the variant for extra scripts the child wants to add at the bottom of the layout.
How do I show validation errors?
Redirect back with the error bag, which validate() already does. In the template, @error('title') prints $message. old('title') keeps the submitted value.