Laravel 4 how to apply title and meta information to each page with blade master page
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Laravel Blade Master Pages: The Cleanest Way to Handle Dynamic SEO Titles and Meta Descriptions
Trying to apply individual titles and meta descriptions across multiple pages when using a master layout in Laravel Blade certainly presents an architectural question. Many developers land on the approach you’ve shown—passing variables from the view into the `@section` block—because it's immediately functional. However, as we scale applications, we need solutions that are not just functional but also clean, maintainable, and adhere to strong separation of concerns.
This post will explore why your current method works, introduce a cleaner architectural pattern, and show you how to manage dynamic SEO data effectively in a Laravel application.
## Understanding the Master Layout Approach
Your setup using `master.blade.php` is fundamentally correct for achieving layout consistency. The goal of the master file is to define the boilerplate HTML structure, including the `` section where the dynamic title and meta tags reside:
```html
{{-- layouts/master.blade.php --}}
{{ $title }}
{{-- Other head elements like CSS links go here --}}
{{ $slot }}
```
This pattern correctly separates the *structure* (in `master.blade.php`) from the *content* (in the child views). The perceived "quick and dirty" feeling often stems from where the `$title` and `$description` variables are being defined in the individual view file, rather than how they are being prepared by the application logic.
## The Cleaner Architectural Solution: Controller as the Source of Truth
The cleaner way to handle this is to ensure that the data powering the page's metadata originates solely from your Controller—the single source of truth for that request. This enforces better separation of concerns (MVC).
Instead of defining `$title` and `$description` directly inside a Blade file, you should prepare these variables in your Controller method and pass them to the view.
### Step 1: Prepare Data in the Controller
In your route-handling controller, fetch the necessary data (perhaps from a database or request parameters) and explicitly pass it to the view.
```php
// app/Http/Controllers/PostController.php
use Illuminate\Http\Request;
class PostController extends Controller
{
public function show($postId)
{
// Example: Fetch data based on the requested post ID
$post = Post::findOrFail($postId);
// Prepare the data for the view
$pageTitle = $post->title;
$metaDescription = $post->excerpt;
return view('posts.show', [
'title' => $pageTitle,
'description' => $metaDescription,
]);
}
}
```
### Step 2: Consume Data in the Blade File
Now, your Blade file becomes purely responsible for rendering the structure and displaying the data passed to it. You simply access the variables provided by Laravel.
```blade
{{-- resources/views/posts/show.blade.php --}}
@extends('layouts.master')
@section('content')
{{ $title }}
{{-- Using the title passed from the controller --}}
{!! nl2br(e($description)) !!} {{-- Using the description passed from the controller --}}
@endsection
```
## Why This is Better Practice
This approach is significantly cleaner because:
1. **Separation of Concerns:** The Controller handles *business logic* and *data retrieval*. The Blade file only handles *presentation*. This adheres to core SOLID principles, which is a hallmark of robust Laravel development.
2. **Reusability:** If you need to display this title/description on an API endpoint or in an email notification, the data is already prepared by the controller, making it reusable across the application architecture, aligning with how sophisticated frameworks like those found in **laravelcompany.com** are designed for scalability.
3. **Maintainability:** If you change how titles are fetched (e.g., integrating SEO tools), you only modify the Controller, leaving your Blade files untouched.
## Conclusion
While passing variables directly into sections is functional, treating the Controller as the definitive source for all presentation data results in a more robust and maintainable application. By ensuring that your controllers prepare the necessary `$title` and `$description` before rendering the view, you establish a scalable foundation. Move away from putting raw data definitions into Blade files, and embrace the power of your Laravel structure to keep your code clean and efficient.