\App\Enums\AIEngine::class, 'is_active' => 'boolean', ]; public function tokens(): HasMany { return $this->hasMany(Token::class); } public function wordToken(): ?Token { return $this->tokens->firstWhere('type', AITokenType::WORD); } public function imageToken(): ?Token { return $this->tokens->firstWhere('type', AITokenType::IMAGE); } public function aiFinance() { return $this->hasMany(AiChatModelPlan::class, 'ai_model_id', 'id'); } public static function planModels(): Collection|array { $planId = getCurrentActiveSubscription()?->getAttribute('plan_id') ?? 0; $query = self::query(); if ($planId == 0) { $query->where('is_selected', 1); } else { $query->whereHas('aiFinance', function ($query) use ($planId) { $query->where('plan_id', $planId); }); } $query->whereHas('tokens', function ($query) { $query->where('type', 'word'); }); return $query->get(); } }