How to get records between two timestamps in Laravel

Categorized as Laravel Tagged

If you need to retrieve records between two timestamps, you can use whereBetween method with a from and to date. You can also to provide a fallback with the null coalescing operator (which was implemented in PHP since version 7+).

Post::whereBetween('published_at', [
  $request->from ?? '1980-01-01',
  $request->to ?? today()->toDateTimeString(),
])->get();

by @ecrmnn

Leave a reply

Your email address will not be published.