PHPackages                             curly-deni/laravel-scopes - 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. [Database &amp; ORM](/categories/database)
4. /
5. curly-deni/laravel-scopes

ActiveLibrary[Database &amp; ORM](/categories/database)

curly-deni/laravel-scopes
=========================

A set of reusable Eloquent global scopes for controlling model visibility based on user ownership and public/private status, designed for seamless integration with Laravel authorization policies.

v1.1.0(1y ago)015[3 PRs](https://github.com/curly-deni/laravel-scopes/pulls)1MITPHPPHP ^8.0CI passing

Since Apr 26Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/curly-deni/laravel-scopes)[ Packagist](https://packagist.org/packages/curly-deni/laravel-scopes)[ Docs](https://github.com/curly-deni/laravel-scopes)[ GitHub Sponsors](https://github.com/curly-deni)[ RSS](/packages/curly-deni-laravel-scopes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (8)Used By (1)

Laravel Scopes
==============

[](#laravel-scopes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/2f4cfd423ab35b5d15a917564f4bf14da7a35da53904fea7b2e558eb4a4c5cab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6375726c792d64656e692f6c61726176656c2d73636f7065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/curly-deni/laravel-scopes)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/29db4f549e0110dd575c80e537b564687877c59ed05112196d49b3648c1f8642/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6375726c792d64656e692f6c61726176656c2d73636f7065732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/curly-deni/laravel-scopes/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/0097f2d86651071c9fbe168cdf4bc4dfdf99150942533ff9cbfcd91435c8b1a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6375726c792d64656e692f6c61726176656c2d73636f7065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/curly-deni/laravel-scopes)

**Laravel Scopes** provides a set of reusable Eloquent global scopes to control model visibility based on user ownership and public/private status.
It is designed for easy integration with Laravel's authorization policies.

---

Features
--------

[](#features)

- ✅ Simple and lightweight integration
- ✅ Reusable Eloquent scopes for ownership and visibility
- ✅ Works seamlessly with Laravel Policies
- ✅ No manual query building required
- ✅ Clean separation of concerns between data and authorization

---

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require curly-deni/laravel-scopes
```

---

Usage
-----

[](#usage)

All traits are located under the `Aesis\Scopes\Traits` namespace.

### Available traits

[](#available-traits)

- `HasOwnershipScope` — restricts access to models owned by the current user (`user_id` field required).
- `HasPublicScope` — restricts access to public models (`public` field required).
- `HasPrivateScope` — restricts access to private models (`private` field required).
- `HasSelfScope` — allows users to see their own models in addition to public/private ones (`user_id` field required).

---

Usage Scenarios
---------------

[](#usage-scenarios)

### 1. Restrict models to the owner only

[](#1-restrict-models-to-the-owner-only)

Use `HasOwnershipScope` when users should only see their own models.

```
use Aesis\Scopes\Traits\HasOwnershipScope;

class Post extends Model
{
    use HasOwnershipScope;
}
```

---

### 2. Restrict models based on public/private status

[](#2-restrict-models-based-on-publicprivate-status)

Use `HasPublicScope` or `HasPrivateScope` to filter models by their visibility flag.
Optionally, add `HasSelfScope` to also allow users to see their own models.

```
use Aesis\Scopes\Traits\HasPublicScope;
use Aesis\Scopes\Traits\HasSelfScope;

class Post extends Model
{
    use HasPublicScope, HasSelfScope;
}
```

---

Trait Selection Guide
---------------------

[](#trait-selection-guide)

Use CaseRequired TraitsUsers should see **only their own models**`HasOwnershipScope`Users should see **only public models**`HasPublicScope`Users should see **only private models**`HasPrivateScope`Users should see **public models** and **their own private models**`HasPublicScope` + `HasSelfScope`Users should see **private models** and **their own private models**`HasPrivateScope` + `HasSelfScope`---

Database Requirements
---------------------

[](#database-requirements)

TraitRequired Field`HasPublicScope``public``HasPrivateScope``private``HasOwnershipScope``user_id``HasSelfScope``user_id`---

Policy Requirements
-------------------

[](#policy-requirements)

When using scopes, you should define the following permissions in your model policies:

MethodPurpose`viewPrivate(User $user)`Allows a user to view **private models** (e.g., admins or users with special roles).`viewForeign(User $user)`Allows a user to view **other users' models** (in addition to their own, if `HasSelfScope` is used).Example policy:

```
class PostPolicy
{
    public function viewPrivate(User $user): bool
    {
        // Allow access to private models for admins
        return $user->is_admin;
    }

    public function viewForeign(User $user): bool
    {
        // Allow users to view models owned by others
        return $user->can('view-others-posts');
    }
}
```

---

Restrictions
------------

[](#restrictions)

- `HasOwnershipScope` **cannot** be combined with any other traits.
- `HasPublicScope` and `HasPrivateScope` **cannot** be used together.
- `HasSelfScope` **requires** either `HasPublicScope` or `HasPrivateScope`.

---

Credits
-------

[](#credits)

- [Danila Mikhalev](https://github.com/curly-deni)
- [All Contributors](../../contributors)

---

License
-------

[](#license)

The MIT License (MIT).
Please see the [LICENSE.md](LICENSE.md) file for more information.

---

Summary
-------

[](#summary)

Laravel Scopes help you automatically filter models by ownership or visibility status without writing repetitive query logic.
Combined with authorization policies, it provides flexible and secure access control at the Eloquent level.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance73

Regular maintenance activity

Popularity6

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 71.4% 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 ~2 days

Total

4

Last Release

374d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f0de0a785e1bbcc02008355c771ba12b545c755b9935a40eb92233f0e5e9eea5?d=identicon)[curly-deni](/maintainers/curly-deni)

---

Top Contributors

[![curly-deni](https://avatars.githubusercontent.com/u/64059451?v=4)](https://github.com/curly-deni "curly-deni (15 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

laraveleloquentlaravel-packageauthorizationscopesvisibilitymodel ownership

###  Code Quality

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/curly-deni-laravel-scopes/health.svg)

```
[![Health](https://phpackages.com/badges/curly-deni-laravel-scopes/health.svg)](https://phpackages.com/packages/curly-deni-laravel-scopes)
```

###  Alternatives

[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17649.9k](/packages/lacodix-laravel-model-filter)[relaticle/custom-fields

User Defined Custom Fields for Laravel Filament

15828.6k](/packages/relaticle-custom-fields)[giacomomasseron/laravel-models-generator

Generate Laravel models from an existing database

484.2k](/packages/giacomomasseron-laravel-models-generator)[dolphiq/laravel-aescrypt

AES encrypt and decrypt Eloquent attributes inspired by elocryptfive

171.7k](/packages/dolphiq-laravel-aescrypt)

PHPackages © 2026

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