---
title: "Querying connections — Connections for Laravel | Roundly"
description: "Check who is connected, walk both directions, filter with active, expired, expiring and permission scopes, and fetch typed models."
url: https://roundly-consulting.com/open-source/docs/connections-for-laravel/querying
language: en
---

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

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

# Querying connections

Ask either side of a connection:

```php
$user->isConnectedTo($team);                       // bool
$user->isConnectedToAny(Team::class);              // bool — a class name or a morph alias
$team->hasConnector($user);                        // bool
$team->hasConnectorFromAny(User::class);           // bool

$user->connections;  // connections this model initiated
$team->connectors;   // connections pointing at this model
```

- isConnectedTo(), isConnectedToAny(), hasConnector() and hasConnectorFromAny() count only active connections while enforce\_active\_on\_check is on.
- isConnectedToAny() and hasConnectorFromAny() take a fully-qualified class name or a registered morph alias — a class name resolves through the morph map, so links stored under an alias are found.
- connections and connectors are plain MorphMany relations over every non-deleted row, whatever its status — chain constraints and eager-load the other side through them.

```php
$user->connections()->with('connectable')->get();   // eager-load the other side
$team->connectors()->with('connector')->get();
$team->connectors()->where('status', 'pending')->count();
```

## Scopes & constrained relations

```php
// Constrained relations (return MorphMany you can chain ->get()/->count()).
$user->activeConnections();                 // accepted + not expired
$user->expiredConnections();                // past expiry
$user->expiringConnections($days = 7);      // expiring within N days
$user->connectionsWithPermission('publish'); // honours '*' / 'posts.*'; active only while enforced

// Eloquent scopes on the model / relation.
$user->connections()->active()->get();
$user->connections()->expiringSoon(14)->get();
$user->connections()->withPermission('posts.edit')->get(); // exact, '*', 'posts.*' — any status

// Across every connector.
Connections::expiring(14)->get();
```

| Scope | Trait relation | Matches |
| --- | --- | --- |
| `active()` | `activeConnections()` | Status accepted and expires\_at null or in the future. |
| `expired()` | `expiredConnections()` | expires\_at set and in the past, whatever the status. |
| `expiringSoon($days = 7)` | `expiringConnections($days = 7)` | expires\_at within the next N days. |
| `withPermission($permission)` | `connectionsWithPermission($permission)` | A stored permission grants it — the exact string, \* or a trailing segment wildcard such as posts.\*. The scope matches any status; the relation counts only active connections while enforce\_active\_on\_check is on. |

The scopes are regular Eloquent scopes on the Connection model, so they work on any query:

```php
use RoundlyConsulting\Connections\Models\Connection;

Connection::query()->active()->withPermission('publish')->count();
Connection::query()->expiringSoon(3)->with(['connector', 'connectable'])->get();
```

## Typed fetches

connectablesOfType() and connectorsOfType() return the connected models themselves instead of Connection rows. Pass a fully-qualified class name or a registered morph alias:

```php
// Fetch the connected models of a given type (FQCN or morph alias), not the Connection rows.
// Like isConnectedTo() / hasConnector(), only active links count while enforce_active_on_check is on.
$user->connectablesOfType(Team::class);   // Collection<Team>
$user->connectablesOfType('team');        // the same, via a morph-map alias
$team->connectorsOfType(User::class);     // Collection<User>
```

Like isConnectedTo() and hasConnector(), typed fetches only count active links while enforce\_active\_on\_check is on; with the flag off they include every non-deleted connection of that type.

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