Q:

How do I get the highest property from a laravel eloquent object

consider the following Model Object:
App\Modules\CharacterSheets\Models\CharacterSheet {#826
  ...
  #attributes: array:39 [
    "id" => 1
    "user_id" => 1
    "race_id" => 1
    "class_id" => null
    "strength" => 35
    "dexterity" => 35
    "agility" => 55
    "intelligence" => 30
    "health" => 120
    "haste" => 0
    "precision" => 0
    "dodge" => 0
    "crit" => 0
    "steal_health" => 5
    "gold" => 0
    "deffense" => 7
    "damage" => 10
    "cast_damage" => 10
    "antique_damage" => 0
    "heal_for" => 10
    "level" => 1
    "train_exp" => 0
    "level_exp" => 200
    "location" => "0, 0"
    "next_level_exp" => 1000
    "next_train_level_exp" => 300
    "max_level" => 500
    "armor_class" => 17
    "block_bonus" => 3
    "hit_bonus" => 2
    "has_to_train" => 0
    "primary_attack_stat" => "health"
    "primary_attack_bonus" => "2.00"
    "penalty_stat" => null
    "penalty" => null
    "damage_subtraction" => null
    "chance_to_attack_twice" => null
    "created_at" => "2019-02-16 01:26:04"
    "updated_at" => "2019-02-16 01:56:42"
  ]
  ...
}

What I would like to do, dynamically, is check which of the following stats are the highest: strength, dexterity, agility, intelligence, health
I want to keep the object as is, but return the property name as a string
So for example with the above object, I should have function that returns health.
Again this has to be dynamic.
I didn't see any laravel collection methods that would do this, so I am asking here as I have no idea how to do this.

A:

Extract property from model and find max value key.
function getHighestProperty(Model $model, array $keys): string
{
    $filter = array_flip($keys);
    $array = collect($model->toArray());
    return $array->intersectByKeys($filter)->sort()->flip()->last();
}

