thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Using BREAD accessors and Laravel model accessors at the same time causes BREAD accessors to fail #5246

Open samik-os opened 3 years ago

samik-os commented 3 years ago

Version information

Description

If an Eloquent model has a Voyager BREAD accessor and a regular accessor at the same time, the BREAD accessor fails to do it's job. For example, if a certain model has getValueAttribute() and getValueEditAttribute(), the Edit BREAD page will likely not get the desired effect by ignoring getValueEditAttribute and opting to use the value from getValueAttribute.

Steps To Reproduce

  1. Create a function getValueAttribute() in a model and have it return a value.
  2. Create another function getValueEditAttribute() in a model and have it return a different value.
  3. Observe how the Edit BREAD ignores the BREAD accessor completely and opts to use the regular accessor.
    // regular accessor
    public function getValueAttribute()
    {
        return "Regular Value";
    }

    // Voyager BREAD accessor
    public function getValueEditAttribute()
    {
        return "Bread Value";
    }

Expected behavior

The BREAD accessor if exists, should reflect it's own value and not the regular accessor.

MrCrayon commented 3 years ago

Yes because we reassign the value but accessors have precedence: https://github.com/the-control-group/voyager/blob/3c2d3149c8a99157d9568717cd6330f6a8af3102/resources/views/bread/edit-add.blade.php#L63

We could change that if with this:

$value = $dataTypeContent->{$row->field.'_'.($edit ? 'edit' : 'add')} ?? $dataTypeContent->{$row->field};

but then all formfields needs to be changed too :thinking: