Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

peffraimidis's avatar

Dynamic Title Livewire with Volt Class API

Hello there, I believe this is simple but I cannot make it :)

In a users.show component I just want to display the user's name as the page title. This is used as a full page component and can be accesed by this route

Volt::route('/users/{user}', 'users.show')->name('users.show');
<?php

use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
use App\Models\User;

new #[Layout('layouts.app')] class extends Component {
    public User $user;

    public function mount(User $user): void
    {
        $this->user = $user;
    }
}; ?>

<div>
    {{ $user->name }}
</div>

I tried the below and all failed

<?php

use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
use App\Models\User;

new #[Layout('layouts.app')] class extends Component {
    public User $user;

    public function mount(User $user): void
    {
        $this->user = $user;
    }

    public function title(): string
    {
        return $this->user->name;
    }
}; ?>

<div>
    {{ $user->name }}
</div>
<?php

use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
use App\Models\User;

new #[Title(fn() => $this->user->name), Layout('layouts.app')] class extends Component {
    public User $user;

    public function mount(User $user): void
    {
        $this->user = $user;
    }
}; ?>

<div>
    {{ $user->name }}
</div>

Any thoughts here my friends ??

0 likes
3 replies
LaryAI's avatar
Level 58

To dynamically set the page title in a Livewire component using the Volt Class API, you need to ensure that the title is set after the component has been initialized and the user data is available. The issue with your attempts is likely due to the timing of when the title is being set. Here's a solution that should work:

  1. Use the Title attribute correctly: The Title attribute should be set after the user data is available. You can achieve this by setting the title in the mount method.

  2. Ensure the title is updated after the user is set: You can use a method to update the title after the user is assigned.

Here's how you can implement it:

<?php

use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Volt\Component;
use App\Models\User;

new #[Layout('layouts.app')] class extends Component {
    public User $user;

    public function mount(User $user): void
    {
        $this->user = $user;
        $this->setTitle();
    }

    #[Title]
    public function setTitle(): string
    {
        return $this->user->name;
    }
};

Explanation:

  • mount Method: This method is called when the component is initialized. Here, you assign the User model to the $user property and then call the setTitle method to set the page title.

  • setTitle Method: This method is responsible for returning the user's name, which will be used as the page title. The #[Title] attribute is used to indicate that this method should be used to set the page title.

This approach ensures that the title is set after the user data is available, resolving the issue you were facing.

Please or to participate in this conversation.