Sometimes you have a shared layout that renders a block — a promotional banner, a sidebar widget, an image — that makes sense on most pages but not all. Rather than duplicating the layout or reaching for JavaScript, you can solve this cleanly with two built-in Blade directives: @section and View::hasSection().
The problem
Say your login layout renders a decorative image at the bottom of every page:
{{-- resources/views/layouts/login.blade.php --}}
@php $loginImage = Contractify::loginImage(); @endphp
@if ($loginImage->src)
<div class="...">
<img src="{{ $loginImage->src }}" alt="{{ $loginImage->alt }}">
</div>
@endif
This is fine for the login and registration pages, but on the OAuth authorization page it's visual noise you'd rather skip.
The solution
Step 1 — Wrap the block in the layout with @unless(View::hasSection(...)):
{{-- resources/views/layouts/login.blade.php --}}
@unless(View::hasSection('hide-login-image'))
@php $loginImage = Contractify::loginImage(); @endphp
@if ($loginImage->src)
<div class="...">
<img src="{{ $loginImage->src }}" alt="{{ $loginImage->alt }}">
</div>
@endif
@endunless
Step 2 — Declare the empty section in the child view that should opt out:
{{-- resources/views/vendor/passport/authorize.blade.php --}}
@extends('layouts.login')
@section('hide-login-image')@endsection
@section('content')
{{-- ... --}}
@endsection
How it works
View::hasSection('hide-login-image') returns true if the currently-rendering child view has declared a section with that name — even an empty one. The @unless then skips the block entirely.
Every other view that extends the layout leaves that section undeclared, so hasSection returns false and the image renders as normal.
Why this is better than the alternatives
- No layout duplication. You keep a single source of truth.
- No conditionals based on route names or controller names. Those couple your layout to your routing.
- The opt-out lives in the view that needs it. Easy to find, easy to remove.
It's a tiny pattern, but it keeps your layouts clean and your child views in control of their own presentation.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.