PHPackages                             crhg/eloquent-exists-relation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. crhg/eloquent-exists-relation

ActiveLibrary

crhg/eloquent-exists-relation
=============================

Add withExits method to check existence of related records with subquery.

v1.1.2(6y ago)0138MITPHPPHP ^7.1

Since Nov 21Pushed 6y ago1 watchersCompare

[ Source](https://github.com/crhg/eloquent-exists-relation)[ Packagist](https://packagist.org/packages/crhg/eloquent-exists-relation)[ RSS](/packages/crhg-eloquent-exists-relation/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (9)Used By (0)

DESCRIPTION
===========

[](#description)

Add an attribute that indicates whether there is an element indicated by the relation specified when acquiring to the Eloquent Model using a subquery. It is similar to `withCount`, which finds the number of elements of a relation, but it differs in obtaining only whether it exists without counting.

INSTALL
=======

[](#install)

```
composer require crhg/eloquent-exists-relation
```

This package is compliant with Package Discovery so no additional configuration is required.

In case of Lumen
----------------

[](#in-case-of-lumen)

Register `EloquentExistsRelationProvider` as follows in `bootstrap/app.php`.

```
$app->register(\Crhg\EloquentExistsRelation\Providers\EloquentExistsRelationProvider::class);
```

USAGE
=====

[](#usage)

If you want to know wheather results from a relationship exists without actually loading them you may use the `withExists` method, which will place a {relation}\_exists column on your resulting models. For example:

```
$posts = App\Post::withExists('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_exists;
}
```

You may add the "exists" for multiple relations as well as add constraints to the queries:

```
$posts = App\Post::withCount(['votes', 'comments' => function ($query) {
    $query->where('content', 'like', 'foo%');
}])->get();

echo $posts[0]->votes_exists;
echo $posts[0]->comments_exists;
```

You may also alias the relationship exists result, allowing multiple exists on the same relationship:

```
$posts = App\Post::withCount([
    'comments',
    'comments as pending_comments_exists' => function ($query) {
        $query->where('approved', false);
    }
])->get();

echo $posts[0]->comments_exists;

echo $posts[0]->pending_comments_exists;
```

TIPS
====

[](#tips)

Since the value of EXISTS may not be a boolean value (for example, 0 or 1 in mysql), it is convenient to cast it explicitly when you want to treat it as a boolean value.

```
    protected $cast = [
        'commensts_exists' => 'bool',
    ];
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~107 days

Total

5

Last Release

2304d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bd1a173eef0d98c0451f8e692aeb623360da4c533d1d15e9d0f6edf5aa4fe12a?d=identicon)[crhg](/maintainers/crhg)

---

Top Contributors

[![crhg](https://avatars.githubusercontent.com/u/8174597?v=4)](https://github.com/crhg "crhg (18 commits)")

### Embed Badge

![Health badge](/badges/crhg-eloquent-exists-relation/health.svg)

```
[![Health](https://phpackages.com/badges/crhg-eloquent-exists-relation/health.svg)](https://phpackages.com/packages/crhg-eloquent-exists-relation)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
