PHPackages                             relaxsd/laravel-model-gates - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. relaxsd/laravel-model-gates

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

relaxsd/laravel-model-gates
===========================

Model gates for Laravel

0.2.0(11mo ago)015MITPHPPHP &gt;=5.4.0

Since Dec 14Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/relaxsd/laravel-model-gates)[ Packagist](https://packagist.org/packages/relaxsd/laravel-model-gates)[ RSS](/packages/relaxsd-laravel-model-gates/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Easy Model Gates
================

[](#easy-model-gates)

This packages allows authorized access to model attributes, so by using the Laravel Gates and Policies, you can reduce the data returned based on authorization.

Install
-------

[](#install)

Pull this package in through Composer.

```
{
    "require": {
        "relaxsd/laravel-model-gates": "0.1.*"
    }
}
```

Usage
-----

[](#usage)

Here's an example of a model gate.

```
use Relaxsd\Gate\ModelGate;

class ProjectGate extends ModelGate {

    public function members()
    {
        if (\Gate::allows('indexForProject', [\App\Member::class, $this->entity])) {
            return $this->entity->members;
        }
        return new \Illuminate\Database\Eloquent\Collection(); // Not authorized: return an empty collection instead.
    }

}
```

Next, on your entity, pull in the `Relaxsd\Gate\ModelGateTrait` trait, which will automatically instantiate your model gate class.

Here's an example - maybe a Laravel `User` model.

```
