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

amitshahc's avatar

Laravel eloquant query to get records out of date range

I want to fetch the count of projects where the current date is not falling between project start/end dates.

public function getTotalProjects($active = false, $running = null)
    {
        $query = Project::query();

        if ($active) {
            $query->active();
        }

        if ($running === true) {
            $query->where('start_date', '<=', Carbon::today());
            $query->where('end_date', '>=', Carbon::today());
        }

        if ($running === false) {
            $query->whereNot(function ($query) {
                $query->where('start_date', '<=', Carbon::today());
                $query->where('end_date', '>=', Carbon::today());
            });
        }

        return $query->get()->count();
}

so if i pass $running = false should give me the count of projects which are not running now.

0 likes
9 replies
Sinnbeck's avatar

Use when() instead

$count = Project::query()
	->when($active, function ($query) {
        $query->active();
    })
	->when($running, function($query, {
            $query->where('start_date', '<=', Carbon::today());
            $query->where('end_date', '>=', Carbon::today());
    }, function($query) {
                $query->where('start_date', '>', Carbon::today());
                $query->where('end_date', '<', Carbon::today());
    })->count();
amitshahc's avatar

@Sinnbeck replacing where with when is nice to know new. but my query is the condition.

if the start/end dates are past dates then your query fails. I need to check if the current date is not falling between the start/end dates.

Sinnbeck's avatar

@amitshahc If you use my code, is it then when running is true or false you get the wrong count?

amitshahc's avatar

@Sinnbeck yeh..tried. it is not giving the correct answer. only works if i pass $running = true

Sinnbeck's avatar

@amitshahc Try explaining again what you expect $running = false to do..

Is this perhaps what you need?

count = Project::query()
	->when($active, function ($query) {
        $query->active();
    })
	->when($running, function($query, {
            $query->where('start_date', '<=', Carbon::today());
            $query->where('end_date', '>=', Carbon::today());
}, function($query) {
  $query->where(function($q) {
        $q->where(function($subQuery) {
                   $subQuery->where( 'start_date', '>', Carbon::today())
                          ->where('end_date', '>', Carbon::today());
             })->orWhere(function($subQuery) {
                   $subQuery->where( 'start_date', '<', Carbon::today())
                          ->where('end_date', '<', Carbon::today());
});
})->count();
amitshahc's avatar
amitshahc
OP
Best Answer
Level 2

@Sinnbeck Thanks for your code. but the when keyword is not giving me correct results somehow. need to find why. but don't have time for now. The below code works well for me.

 $query = Project::query();

        if ($active) {
            $query->active();
        }

        if ($running === true) {
            $query->where('start_date', '<=', Carbon::today());
            $query->where('end_date', '>=', Carbon::today());
        }

        if ($running === false) {
            $query->where(function ($query) {
                $query->orWhere(function ($query) {
                    $query->where('start_date', '<', Carbon::today());
                    $query->where('end_date', '<', Carbon::today());
                });
                $query->orWhere(function ($query) {
                    $query->where('start_date', '>', Carbon::today());
                    $query->where('end_date', '>', Carbon::today());
                });
            });
        }

        return $query->get()->count();
Sinnbeck's avatar

@amitshahc I might have made a mistake in the code, as coding in the browser with no IDE isnt easy.. But when takes 3 arguments..

  1. The boolean
  2. The true query
  3. The false query
amitshahc's avatar

@Sinnbeck yeh I assumed so. but there is one more case which I need to cover here. $running=null. in that case both of the queries related to $running should not run. so it's a better fit for the where clause. Thanks.

Please or to participate in this conversation.