tam5
tam5

Reputation: 3237

Laravel Eloquent where with calculation on value

I have a column in my table called date which is of type TIMESTAMP.

I am trying to add a very simply scope which will return all results that have a date of tomorrow.

I think I want to be able to do something like this:

public function scopeTomorrow($query)
{
    return $query->where('date', function ($date) {
        return \Carbon\Carbon::parse($date)->isTomorrow();
    });
}

But the $date variable is currently just the query builder.

How can I create a where statement which accepts the value and perfroms some sort of check on it?

Upvotes: 3

Views: 479

Answers (2)

Alexey Mezenin
Alexey Mezenin

Reputation: 163948

You can do this:

return $query->where('date', '>=', Carbon::tomorrow())
             ->where('date', '<=', Carbon::tomorrow()->endOfDay());

Or:

return $query->whereBetween('date', [Carbon::tomorrow(), Carbon::tomorrow()->endOfDay()]);

Also, probably you want to add date to $dates variable.

Upvotes: 1

Yada
Yada

Reputation: 31235

You can do something like this:

public function scopeTomorrow($query)
{
    $tomorrow = \Carbon\Carbon::now()->addDay(1);

    return $query->where('date', '=', $tomorrow);
}

Upvotes: 0

Related Questions