relation = $relation; $this->whereCondition = $whereCondition; $this->whereType = $whereType; $this->entityId = $entityId; } public function apply($query) { $context = CompanySettings::getContext(); $emailIds = EmailContext::where('entity_id',$this->entityId) ->where('company_id',$context->get('company_id')) ->where('entity_type',Contact::class)->pluck('email_id'); $query->whereIn('emails.id',$emailIds); } }