Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

query relationship existance with scope

Here are my model classes:

Conversation:

public function reports()
{
    return $this->hasMany('App\ConversationReport');
}

ConversationReport

public function scopeActiveReports($query)
{
    return $query->whereNull('processed_by');
}

Is there a way I can check if a conversation has an active report? I tried using:

$this->reports()->activeReports()->exists();

working fine, but it will not limit the query, meaning it will load all active reports, I only want to perform a check if there is at least an active report.

Is the only way to do so using has() and move the scopeActiveReportsinto the Conversation model? From my point of view the scope should stay inside the ConversationReportmodel.

0 likes
1 reply
STEREOH's avatar

exists() generates a request with a SELECT EXISTS( your query) , this should not load all of your results AFAIK.

If you are not convinced, you can always do :

$this->reports()->activeReports()->take(1)->exists();

Please or to participate in this conversation.