hasMany(Audition::class)->orderBy('score_order'); } public function subscores(): HasMany { return $this->hasMany(SubscoreDefinition::class); } /** * Validate a set of subscores. Expects an array in the form of subscore_id => score * @param array $prospective_score * @return string */ public function validateScores(Array $prospective_score) { foreach ($this->subscores as $subscore) { if (! array_key_exists($subscore->id,$prospective_score)) return "A score must be provided for " . $subscore->name; if (is_null($prospective_score[$subscore->id])) return "A score must be provided for " . $subscore->name; if ($prospective_score[$subscore->id] > $subscore->max_score) return "The " . $subscore->name . " score must be less than or equal to " . $subscore->max_score; } $subscore_ids = $this->subscores->pluck('id')->flip()->all(); $diff = array_diff_key($prospective_score, $subscore_ids); if (!empty($diff)) return "Invalid scores submitted"; return 'success'; // TODO this probably needs to be rewritten as a validation rule } }