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

yusuf128's avatar

Single Eloquent Query

My task is done but problem with single query.So I want to count data with current date and future date and with status in single query .

My Scopefunctions

public function scopeAction($q){
    return $q->wheredueDate(carbon::now()->toDateString())->whereTaskStatus(self::TASK_STATUS_INCOMPLETE);
}

public function scopeSnooze($q){
    return $q->where('due_date','>',carbon::now()->toDateString())->whereTaskStatus(self::TASK_STATUS_INCOMPLETE);
}

public function scopeComplete($q){
    return $q->whereTaskStatus(self::TASK_STATUS_COMPLETE);
}

And my query 

    $countByStatus['actionTotal'] = Tasks::active()->action()->count();
        $countByStatus['snoozeTotal'] = Tasks::active()->snooze()->count();
        $countByStatus['completeTotal'] = Tasks::active()->complete()->count();
0 likes
2 replies
Tray2's avatar

I would do something like this

$result = DB::select("SELECT (SELECT count(*) FROM task WHERE staus = 'active' active_tasks), (SELECT count(*) FROM tasks WHERE status = 'sleeping') sleeping_tasks, SELECT count(*) FROM tasks WHERE status = 'completed'") ;
yusuf128's avatar

Thanks for the anwer, But how can by eloquent

Please or to participate in this conversation.