getteraccessorfilamentphp

Does FilamentPHP execute Models accessors when editing or viewing a row?


I have a critical concern, which might have a dumb & simple solution I cannot find, or it might open a Pandora box.

If you define an accessor in a model, and then you try to use it in a filamentPHP resource, the accessor seems to execute fine and show what you want in the index resource (when listing all records), but never when editing or viewing a specific record.

Reproducing the issue is quite simple. For simplicity let's take the typical User model of any Laravel installation.

  1. Let's create an accessor like the following: public function getTadaAttribute() { return 'tada! '.$this->name; }

  2. Then in the UserResource.php, just add to your regular form schema something like:

    Forms\Components\TextInput::make('tada')

and also in your table method:

`Tables\Columns\TextColumn::make('tada')`

Done. Now try it. you will see that your record listing does show this new column, but when you edit or view one of the records it does not show up... WHY!?


Solution

  • Adding the accessor attribute to the model's $appends property should resolve your issue, as the attribute will be added to your model's json and array representations.

        <?php
          
        class User extends Model
        {
            ...
    
            protected $appends = ['tada'];
    
            protected function tada(): Attribute
            {
               return new Attribute(
                get: fn () => 'tada! '.$this->name;,
               );
            }
    
            ...
    
         }
    

    source