Displaying Custom Model attributes in a table

Before I upgraded to V3 I was able to access my custom attribute by like so
TagColumn::make('triggers.definedName)

but since upgrading this seems to no longer work.
TextColumn::make('triggers.definedName')
    ->badge(),


Here is my attribute
    public function definedName(): Attribute
    {
        return Attribute::make(
            get: function (mixed $value, array $attribute) {
                if ($attribute['machine_status_id'] != null) {
                    return $attribute['line_status->name'];
                }

                if ($attribute['machine_state_id'] != null) {
                    return $attribute['machine_state->name'];
                }

                return $attribute['name'];
            }
        );
    }


I can access it in Tinker
$t = Trigger::first()
[!] Aliasing 'Trigger' to 'App\Models\Trigger' for this Tinker session.
= App\Models\Trigger {#7658
    id: "1",
    criteria: "max",
    value: "5",
    unit_of_measure: "parts",
    created_at: "2023-01-24 17:32:09.333",
    updated_at: "2023-12-11 16:33:13.657",
    machine_status_id: null,
    task_id: null,
    machine_state_id: null,
    name: "First Occurrence",
  }

> $t->definedName;
= "First Occurrence"
Solution
Fixed it by changing my attribute to look like this
public function getDefinedNameAttribute()
    {
        if ($this->machine_status_id != null) {
            return $this->line_status->name;
        }

        if ($this->machine_state_id != null) {
            return $this->machine_state->name;
        }

        return $this->name;
    }


Also in the model

protected $appends = ['definedName'];
Was this page helpful?