Search code examples
laravellaravel-filamentfilamentphp

How to change Filament Placeholder content on LIVE?


I am using Filament 3.2. How to update Placeholder content on live?

Select::make('authors')
    ->relationship('authors', 'name')
    ->live()
    ->afterStateUpdated(function (string $operation, $state, Forms\Set $set, Forms\Get $get) {

    $user = User::find($state[0]);
    if ($user) {
        $userEmail = $user->email;
        $set('authoremail', $userEmail);
    }
    }),

// TextInput::make('authoremail')
//     ->label("Author's Email")
//     ->disabled(),

Placeholder::make('authoremail')

If it is TextInput, it works. But it doesn't work on Placeholder. How can I make it works on Placeholder?


Solution

  • https://filamentphp.com/docs/3.x/forms/layout/placeholder#dynamically-generating-placeholder-content

    By passing a closure to the content() method, you may dynamically generate placeholder content. [...]

    use Filament\Forms\Components\Placeholder;
    use Filament\Forms\Get;
     
    Placeholder::make('total')
        ->content(function (Get $get): string {
            return '€' . number_format($get('cost') * $get('quantity'), 2);
        })
    

    So when applying it to your code, I would try to do something like this:

    Select::make('authors')
        ->relationship('authors', 'name')
        ->live(),
    
    Placeholder::make('authoremail')
        ->content(function (Get $get): ?string {
            $user = User::find($get('authors')[0] ?? null);
    
            return $user?->email;
        }),