*/ protected $fillable = [ 'user_id', 'alias', 'spend', 'budget_id', 'blocked', 'metadata', 'budget_started_at', 'next_budget_reset_at', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'spend' => 'double', 'blocked' => 'boolean', 'metadata' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'budget_started_at' => 'datetime', 'next_budget_reset_at' => 'datetime', ]; } /** * Get the API keys for this user. */ public function apiKeys() { return $this->hasMany(ApiKey::class, 'user_id', 'user_id'); } /** * Get the usage logs for this user. */ public function usageLogs() { return $this->hasMany(UsageLog::class, 'user_id', 'user_id'); } /** * Get the budget for this user. */ public function budget() { return $this->belongsTo(Budget::class, 'budget_id', 'budget_id'); } /** * Scope a query to only include active users. */ public function scopeActive($query) { return $query->where('blocked', false); } /** * Scope a query to only include blocked users. */ public function scopeBlocked($query) { return $query->where('blocked', true); } /** * Get the formatted spend amount. */ public function getSpendFormattedAttribute() { return '$' . number_format($this->spend, 2); } /** * Get the total number of requests. */ public function getTotalRequestsAttribute() { return $this->usageLogs()->count(); } /** * Get the total number of tokens used. */ public function getTotalTokensAttribute() { return $this->usageLogs()->sum('total_tokens'); } }