PHPackages                             dmdboi/laravel-autoscopes - 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. dmdboi/laravel-autoscopes

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

dmdboi/laravel-autoscopes
=========================

Automatically generate scope traits for Laravel Models based on foreign key relationships.

1.0.1(2w ago)01↓100%MITPHPPHP ^8.2CI passing

Since Mar 5Pushed 2w agoCompare

[ Source](https://github.com/dmdboi/laravel-autoscopes)[ Packagist](https://packagist.org/packages/dmdboi/laravel-autoscopes)[ Docs](https://github.com/dmdboi/laravel-autoscopes)[ RSS](/packages/dmdboi-laravel-autoscopes/feed)WikiDiscussions develop Synced today

READMEChangelog (2)Dependencies (13)Versions (4)Used By (0)

Laravel Auto Scopes
===================

[](#laravel-auto-scopes)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f54716362572402f8763ae09bc9ad0e2a1db86677675aaad24d42fb398fda99d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646d64626f692f6c61726176656c2d6175746f73636f7065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dmdboi/laravel-autoscopes)[![GitHub Tests Action Status](https://camo.githubusercontent.com/79b83c1542b4aef944df2ffe3efc13672b336fe58e165bf607b7c6ee82a4bff0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646d64626f692f6c61726176656c2d6175746f73636f7065732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/dmdboi/laravel-autoscopes/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/1bd8c726b0972e48f62486971fa9fec8cb64a86235d56cc36aee75e6d5f1a58e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f646d64626f692f6c61726176656c2d6175746f73636f7065732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/dmdboi/laravel-autoscopes/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/ef79281d4d3f203cd1390365a9250ad26e6bfac88b4e060de0b248d12efea11c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646d64626f692f6c61726176656c2d6175746f73636f7065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dmdboi/laravel-autoscopes)

Automatically generate type-safe scope traits for Laravel models based on foreign key relationships.

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

[](#installation)

```
composer require dmdboi/laravel-autoscopes --dev
```

Quick Start
-----------

[](#quick-start)

Generate scope traits for your models:

```
# Creates HasUserScopes trait and applies to models with user_id column
php artisan scopes:generate User

# Use the generated scopes
Post::forUser(1)->get();
Post::forUserIn([1, 2, 3])->get();
Post::exceptUser(1)->get();
Comment::forUser(1)->forPost(5)->get();
```

How It Works
------------

[](#how-it-works)

Given models with foreign keys:

```
class Post extends Model {
    protected $fillable = ['user_id', 'title', 'content'];
}

class Comment extends Model {
    protected $fillable = ['user_id', 'post_id', 'content'];
}
```

Running `php artisan scopes:generate User`:

1. Creates `HasUserScopes` trait in `app/Models/Traits/`
2. Auto-detects Post and Comment have `user_id` column (via database schema or $fillable)
3. Applies trait to both models automatically

Generated trait includes:

```
