bestFirst() */ #[Fillable(['predicted_for', 'source', 'direction', 'confidence', 'reasoning', 'generated_at'])] class PricePrediction extends Model { /** @use HasFactory */ use HasFactory; public $timestamps = false; protected function casts(): array { return [ 'predicted_for' => 'date', 'source' => PredictionSource::class, 'direction' => TrendDirection::class, 'confidence' => 'integer', 'generated_at' => 'datetime', ]; } /** * Order by source quality: llm_with_context → llm → ewma. * Use this whenever reading the "best" prediction for a given date. * * @param Builder $query * @return Builder */ public function scopeBestFirst(Builder $query): Builder { $priority = implode(', ', array_map( fn (string $v) => "'$v'", [PredictionSource::LlmWithContext->value, PredictionSource::Llm->value, PredictionSource::Ewma->value], )); return $query->orderByRaw("FIELD(source, $priority)"); } }