---
title: "Query scopes — Contacts for Laravel | Roundly"
description: "Composable Contact scopes — by owner, category, kind, primary, verification status, free-text search and position order."
url: https://roundly-consulting.com/open-source/docs/contacts-for-laravel/query-scopes
language: en
---

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

[Contacts for Laravel](https://roundly-consulting.com/open-source/docs/contacts-for-laravel.md)

# Query scopes

Every scope works on Contact::query() and on an owner’s contacts() relation, and they combine freely:

```php
use RoundlyConsulting\Contacts\Enums\ContactType;
use RoundlyConsulting\Contacts\Models\Contact;

Contact::query()->forOwner($user)->get();
Contact::query()->forOwner(owner: $user, includeAlsoWithoutOwner: true)->get();
Contact::query()->withoutOwner()->get();
Contact::query()->forCategory('Utility')->get();
Contact::query()->withoutCategory()->get();

Contact::query()->ofType(ContactType::Email)->get();   // or ->ofType('email')
Contact::query()->primary()->get();
Contact::query()->verified()->get();                   // ->verified(false) for unverified
Contact::query()->pendingVerification()->get();        // unverified, token issued
Contact::query()->search('alice')->get();              // name / value / label LIKE
Contact::query()->ordered()->get();                    // by position, then id
```

## Scope reference

| Scope | Matches |
| --- | --- |
| `forOwner(Model $owner, bool $includeAlsoWithoutOwner = false)` | Contacts of one owner, optionally plus ownerless ones. |
| `withoutOwner()` | Contacts with no owner. |
| `forCategory(string $category)` | Contacts in a category. |
| `withoutCategory()` | Contacts with no category. |
| `ofType(ContactType\|string $type)` | One kind — built-in or a registered custom kind. |
| `primary()` | Primary contacts only. |
| `verified(bool $verified = true)` | Verified — or unverified with false. |
| `pendingVerification()` | Unverified contacts holding a token. |
| `search(string $term)` | Case-insensitive contains match on name, value or label; wildcards escaped; empty term is a no-op. |
| `ordered()` | Sort by position, then id. |

## Combining scopes

```php
// A company's verified supplier emails matching a domain, in position order
Contact::query()
    ->forOwner($company)
    ->forCategory('Suppliers')
    ->ofType(ContactType::Email)
    ->verified()
    ->search('example.com')
    ->ordered()
    ->get();
```

search() uses ILIKE on PostgreSQL and LIKE elsewhere, with % and \_ in the term escaped so they match literally on every driver.

## 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/contacts-for-laravel.md)
