laravel 5.4 how to make page title with dynamic
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# 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 --}}
List of Artists for Letter {{ $letter }}
-
@foreach ($artists as $artist)
- {{ $artist->name }} @endforeach