laravel 5.4 how to make page title with dynamic

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Dynamic Page Titles in Laravel: Mastering Route Parameters for SEO

As a senior developer working with the Laravel ecosystem, one of the most common tasks we face is dynamically generating content based on user input or route parameters. Today, we are diving into a practical scenario: how to leverage dynamic data from a route parameter to construct a meaningful and SEO-friendly page title in your Blade view.

If you have set up a dynamic route like /artists/{letter} and want the resulting page title to reflect that specific letter (e.g., "Artists beginning with A"), the key lies not just in accessing the variable, but in ensuring that data flows correctly from your controller through to your view. This guide will walk you through the exact mechanism for achieving this dynamic page title seamlessly in Laravel.

Understanding the Data Flow

Your setup involves three main components: the Route, the Controller, and the View. The challenge usually arises in bridging the gap between the data fetched in the controller and how it is presented in the Blade file.

Here is a review of your existing setup:

Route Definition:

php
Route::get('/artists/{letter}', 'HomeController@showArtist')
    ->where('letter', '[A-Za-z]+')
    ->name('list');

Controller Method:

php
public function showArtist($letter){
    $artists = Artist::where('name', 'like', $letter.'%')->get();
    return view('front.list', compact('artists'));
}

Notice that your controller successfully retrieves the $letter parameter and uses it to query the database, fetching the relevant artists. The variable $letter is available within the scope of the showArtist method. The next step is ensuring this specific value is passed to the view where you intend to set the title.

Implementing Dynamic Titles in Blade

To use a variable from the controller inside your Blade file (view), you must pass it explicitly using the compact() function or by passing an associative array. Since you are already using compact('artists'), we need to extend this to include our dynamic letter variable.

Step 1: Passing the Dynamic Data from the Controller

Modify your showArtist method to include the $letter variable in the compact array. This makes it available to the view.

php
public function showArtist($letter){
    $artists = Artist::where('name', 'like', $letter.'%')->get();
    // Pass both artists and the dynamic letter to the view
    return view('front.list', compact('artists', 'letter')); 
}

Step 2: Accessing the Variable in the Blade File

Now that $letter is available in the view context, you can use standard Blade syntax to inject it into your @section('title').

In your front/list.blade.php file, replace your existing section with the following structure:

html
@section('title', 'Artists beginning with ' . ucfirst($letter))

{{-- Rest of your page content --}}

<h1>List of Artists for Letter {{ $letter }}</h1>

<ul>
    @foreach ($artists as $artist)
        <li>{{ $artist->name }}</li>
    @endforeach
</ul>

Explanation of the Fix:

  1. Accessing the Variable: When you use compact('artists', 'letter') in your controller, Laravel makes $letter available to the view. You access it directly within the Blade file using $letter.
  2. String Concatenation: To achieve the desired formatting ("Artists beginning with A"), we use PHP's string concatenation operator (.) to combine static text with the dynamic variable.
  3. Formatting (Best Practice): I used ucfirst($letter) to ensure that if the route passes 'a', the title reads "Artists beginning with A" instead of "Artists beginning with a," improving readability and SEO quality.

Best Practices and Laravel Context

This approach demonstrates fundamental data handling in Laravel. When dealing with complex relationships or large datasets, remember that Eloquent is your best friend. As you scale up your application—perhaps moving towards more advanced features like Route Model Binding (which simplifies fetching models directly by ID) or using dedicated package management—understanding how variables travel from the Model layer up to the View layer becomes even more crucial. For deeper dives into efficient data retrieval strategies within Laravel, always refer back to official documentation. You can find excellent resources detailing framework architecture at https://laravelcompany.com.

Conclusion

By correctly managing the data flow from your controller to your view using compact(), you unlock the ability to create truly dynamic and personalized user experiences. By ensuring that route parameters are explicitly passed as variables, you can easily construct rich, context-aware page titles that not only improve user experience but also enhance your Search Engine Optimization (SEO). Happy coding!