All packages
Advertisements for Laravel
Query scopes
Expressive scopes answer the common questions without hand-written where clauses:
use RoundlyConsulting\Advertisements\Facades\Advertisements;
use RoundlyConsulting\Advertisements\Models\Advertisement;
Advertisement::query()->published()->get(); // published_at reached, not archived
Advertisement::query()->active()->get(); // published AND not expired
Advertisement::query()->scheduled()->get(); // published_at in the future
Advertisement::query()->expired()->get(); // expires_at passed
Advertisement::query()->draft()->get(); // never published
Advertisement::query()->archived()->get(); // explicitly archived
Advertisement::query()->forAuthor($user)->get();
// The facade exposes a fresh builder for chaining:
Advertisements::query()->active()->get();Every scope
| Scope | Matches |
|---|---|
published() | published_at reached, not archived — includes ads past expires_at. |
active() | Published and not expired — what you serve. |
scheduled() | published_at in the future, not archived. |
expired() | expires_at passed, not archived. |
draft() | Never published (no published_at), not archived. |
archived() | Explicitly archived. |
forAuthor($model) | Owned by the given polymorphic author. |
forPlacement($placement) | Attached to a placement — model, id or slug. |
inCategory($category, $includeDescendants = false) | In a category — model, id or slug — optionally with nested ones. |
targetedAt($viewer) | Served to a viewer’s location (see Geo-targeting). |
whereSlug($slug, locale: null) | By slug, from sluggable-for-laravel. |
Composing
Scopes compose with each other and with plain Eloquent:
Advertisement::query()
->active()
->forPlacement('sidebar')
->inCategory('vehicles', includeDescendants: true)
->forAuthor($user)
->latest('published_at')
->paginate(20);Advertisements::query() returns a fresh builder for the configured model (see Custom models); Advertisements::active() is query()->active(), and Advertisements::in($placement) adds forPlacement() on top.
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 cryptoBy donating, you agree to our donation terms.
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.