简体   繁体   中英

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?

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.

You can do something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM