F
Filament15mo ago
Anto

Problem displaying laravel model $hidden fields in Filament forms

Hello, FIlament version: 3.0.53 PHP version: 8.1.4 My problem is that I have certain fields in my Laravel model set to be hidden using the $hidden property. When I try to edit these fields using Filament forms, they are not visible, as expected. I'm looking for a way to make them visible in the Filament forms without altering the $hidden property globally. The easy but not clean solution is duplicate the model without the $hidden property and use the duplicated model for Filament. The approach I'm currently taking, in my EditRecord class I added two methods "beforeFill" and "afterFill":
protected function beforeFill(): void
{
$record = $this->getRecord();
$record->makeVisible(['outstanding_order']);
}

protected function afterFill(): void
{
$this->refreshFormData(['outstanding_order']);
}
protected function beforeFill(): void
{
$record = $this->getRecord();
$record->makeVisible(['outstanding_order']);
}

protected function afterFill(): void
{
$this->refreshFormData(['outstanding_order']);
}
Note: I added the refreshFormData in the afterFill() because after looking into the fillFormWithDataAndCallHooks method, $data remains unchanged after calling the beforeFill hook. Perhaps if there was a $this->callHook('beforeFetchingAttributes'); just before executing $data = $this->getRecord()->attributesToArray(); in the fillForm() method, it might provide more flexibility for such scenarios. I understand that having too many hooks can lead to complexity, and there might be reasons for not having implemented it this way. Just a suggestion for consideration 😅 Does anyone have a better solution or suggestions for this problem? Any help would be appreciated. Thanks in advance!
Solution:
I had a similar need, In my case I wanted to display a hidden property in a form. I ended up using: ``` TextInput::make('secret')...
Jump to solution
2 Replies
Solution
Eugen Pașca
Eugen Pașca15mo ago
I had a similar need, In my case I wanted to display a hidden property in a form. I ended up using:
TextInput::make('secret')
->visibleOn('view')
->formatStateUsing(fn (Client $client) => $client->secret),
TextInput::make('secret')
->visibleOn('view')
->formatStateUsing(fn (Client $client) => $client->secret),
Anto
AntoOP15mo ago
->formatStateUsing worked for me, thanks!
Want results from more Discord servers?
Add your server