---
title: "SQL scopes — Opening Hours for Laravel | Roundly"
description: "Filter owners that are open now or open throughout a span directly in SQL, using opt-in materialized intervals."
url: https://roundly-consulting.com/open-source/docs/opening-hours-for-laravel/sql-scopes
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Opening Hours for Laravel](https://roundly-consulting.com/open-source/docs/opening-hours-for-laravel.md)

# SQL scopes

To filter owners in SQL — “clinics open now” on a directory page — enable materialization, schedule the command daily and use the scopes:

```bash
OPENING_HOURS_MATERIALIZE=true
OPENING_HOURS_MATERIALIZE_DAYS_AHEAD=60
OPENING_HOURS_MATERIALIZE_DAYS_BEHIND=1
```

```php
// routes/console.php
use Illuminate\Support\Facades\Schedule;

Schedule::command('opening-hours:materialize')->daily();

// anywhere
Clinic::query()->whereOpenAt(now())->get();
Clinic::query()->whereOpenThroughout($start, $end, 'pickup')->get();
```

- The opening\_hours\_intervals table holds coalesced opening periods as UTC rows for \[today − days\_behind, today + days\_ahead\], evaluated as the owner sees them — its timezone hook and dynamic providers apply, a soft-deleted owner included.
- Every change queues a unique MaterializeIntervalsJob for that calendar on materialize.connection and materialize.queue — none while one is still pending.
- The scopes accept instants from now − days\_behind to now + days\_ahead − 1 day — the day held back covers the time since the last daily run. Instants outside throw OutsideMaterializedHorizonException instead of silently answering “closed”.
- whereOpenAt($at, ?$calendar) and whereOpenThroughout($start, $end, ?$calendar) add an owner-correlated EXISTS; trashed calendars hold no intervals.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/opening-hours-for-laravel.md)
