Are the dates in show_from and show_till column Carbon instances?
You need to declare them inside the 'items' Model.
protected $dates = ['show_from', 'show_till'];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
So I was wondering if it's possible to check if the current time is between 2 date fields in the database?
I was thinking of something like this, but this isn't working:
Screen::find($id)->items()
->where('show_from', '<=', Carbon::now())
->where('show_till', '>=', Carbon::now())->get();
Use a closure?
->where(function ($q) {
$q->where('show_from', '<=', Carbon::now());
$q->where('show_till', '>=', Carbon::now());
})
That will ensure that these two conditions must be true -- independent of any other where clauses.
In other words, when the query string is written, these will be within a set of parenthesis:
SELECT * FROM Foo WHERE (show_from <= whatever AND show_till >= whatever)
Please or to participate in this conversation.