F
Filamentβ€’13mo ago
Michal

Show hidden model attributes

What I am trying to do: I am trying to show hidden model attributes in form. What I did: Searched whole documentation and searched here in questions, but still can't solve it. My issue/the error: Hidden attributes is not accessible trough form. Is there any way make it visible like in Laravel when using $model->makeVisible(['provider_id', 'provider'])? Code: // Model
protected $hidden = ['provider', 'provider_id', 'content_id'];
protected $hidden = ['provider', 'provider_id', 'content_id'];
// Filament form
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make("title"),
TextInput::make("poster_image"),
TextInput::make("type"),
TextInput::make("provider"),
TextInput::make("provider_id"),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make("title"),
TextInput::make("poster_image"),
TextInput::make("type"),
TextInput::make("provider"),
TextInput::make("provider_id"),
]);
}
Solution:
Try this:
TextInput::make('provider')
->visibleOn('view')
->formatStateUsing(fn (Model $item) => $item->provider),
TextInput::make('provider')
->visibleOn('view')
->formatStateUsing(fn (Model $item) => $item->provider),
...
Jump to solution
5 Replies
AiAe
AiAeβ€’11mo ago
I'm facing the same issue with few hidden fields , is there no way to display them? I tried to use makeVisible() in getModel and getEloquentQuery but I don't think thats possible. Also $form->makeVisible() is not working $form->getRecord()->setVisible(['name']) does set the record to have it visible, but filament still wont show it The above solution does not show the name when doing toArray() to test, but this does $form->getRecord()->makeVisible(['name']); but filament still wont show it
Solution
Sector
Sectorβ€’11mo ago
Try this:
TextInput::make('provider')
->visibleOn('view')
->formatStateUsing(fn (Model $item) => $item->provider),
TextInput::make('provider')
->visibleOn('view')
->formatStateUsing(fn (Model $item) => $item->provider),
Of course, you need to replace the Model with your specified model. πŸ™‚
Patrick
Patrickβ€’11mo ago
let me do a quick test
TextInput::make("provider")
->visible(fn ($record) => $record ? $record->makeVisible('provider') : null),
TextInput::make("provider")
->visible(fn ($record) => $record ? $record->makeVisible('provider') : null),
wouldnt this work
AiAe
AiAeβ€’11mo ago
Both methods work, thanks i will probably stick with Sector's suggestion because it feels more readable @Michalsince u made the help card i suppose its resolved now ^^ Thanks again to both of you
Michal
MichalOPβ€’11mo ago
Thank you, my solution was creating a resource and made the attributes visible πŸ˜€
Want results from more Discord servers?
Add your server