PHPackages                             malhal/laravel-createdbypolicy - 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. [Framework](/categories/framework)
4. /
5. malhal/laravel-createdbypolicy

ActiveLibrary[Framework](/categories/framework)

malhal/laravel-createdbypolicy
==============================

Simple security for Laravel models

v1.0.1(9y ago)08MITPHPPHP &gt;=5.6.4

Since Sep 13Pushed 9y ago1 watchersCompare

[ Source](https://github.com/malhal/Laravel-CreatedByPolicy)[ Packagist](https://packagist.org/packages/malhal/laravel-createdbypolicy)[ RSS](/packages/malhal-laravel-createdbypolicy/feed)WikiDiscussions master Synced 1mo ago

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

Laravel-CreatedByPolicy
=======================

[](#laravel-createdbypolicy)

A trait that implements authorization using simple booleans in your policy classes. It provides create, write and read modes and world, authenticated and creator user-based security.

It requires [Laravel-CreatedBy](https://github.com/malhal/Laravel-CreatedBy) which provides the functionality of tracking which user created and updated models, so please follow its setup instructions including adding the table columns and relations for the created\_by\_id and updated\_by\_id.

Configuration
-------------

[](#configuration)

Enter the standard artisan command to make a policy (replacing the name with your model name):

```
php artisan make:policy VenuePolicy

```

As normal, add the policy to AuthServiceProvider policies array, e.g.

```
protected $policies = [
    'App\Venue' => 'App\Policies\VenuePolicy',
];

```

Now for something different, in your project open the generated policy class and add the following trait:

```
use CreatedByPolicy;

```

The default security params are shown in the CreateByPolicy trait php file for easy access, and look like this:

```
// const WORLD_CREATE = false;
// const WORLD_READ = true;
// const WORLD_WRITE = false;
// const AUTHENTICATED_CREATE = true;
// const AUTHENTICATED_READ = false;
// const AUTHENTICATED_WRITE = false;
// const CREATOR_READ = false;
// const CREATOR_WRITE = true;

```

You can override the default security levels by implementing the constants, e.g. to prevent guests from reading:

```
const WORLD_READ = true;

```

Now you can use Laravel's built-in [authorization](https://laravel.com/docs/5.3/authorization) methods as normal, exceptions will be thrown in the case of no access, and for creatorRead a global scope will be added to limit queries to only records created by the currently authenticated user, for example:

```
protected function create(Request $request){
    $this->authorize('create', Venue::class);
    //
}

```

Laravel has a limitation that policies only works if there is a user, since our policies include world read/write (guest access) we need to workaround it with a provider. In `config/app.php` add to the providers array:

```
Malhal\CreatedByPolicy\WorldServiceProvider::class

```

All this does is if `Auth::user()` is null it sets it to a new User which has no ID. This means that `Auth::check()` will no longer work as designed, and instead you have to check for guest with `is_null(Auth::user()->getKey())`.

You shouldn't need to but just in case, disable security checking on a query that alreay had the global scope applied remove it with:

```
$query->withoutCreatedByPolicy()

```

There is an issue when using the CreatedBy trait on the User model with auto-increment. MySQL cannot insert a new record and include a foreign key to the newly created ID. This issue doesn't occur if using UUID for keys, however you can still use the policy with auto-increment by implementing this method in your User model, which assumes user records are always created by the user:

```
public function getCreatedByForeignKey(){
    return $this->getKey();
}

```

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

[](#installation)

[PHP](https://php.net) 5.6.4+ and [Laravel](http://laravel.com) 5.3+ are required.

To get the latest version of Laravel CreatedByPolicy, simply require the project using [Composer](https://getcomposer.org):

```
$ composer require malhal/laravel-createdbypolicy dev-master
```

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 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 ~20 days

Total

2

Last Release

3555d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/916912?v=4)[Malcolm Hall](/maintainers/malhal)[@malhal](https://github.com/malhal)

---

Top Contributors

[![malhal](https://avatars.githubusercontent.com/u/916912?v=4)](https://github.com/malhal "malhal (34 commits)")

---

Tags

frameworklaravelsecurityuserpermissionsLaravel SecuritymodelsMalcolm HallmalhalCreatedByupdatedByCreatedByPolicyLaravel-CreatedByPolicy

### Embed Badge

![Health badge](/badges/malhal-laravel-createdbypolicy/health.svg)

```
[![Health](https://phpackages.com/badges/malhal-laravel-createdbypolicy/health.svg)](https://phpackages.com/packages/malhal-laravel-createdbypolicy)
```

###  Alternatives

[laravel/laravel

The skeleton application for the Laravel framework.

84.6k62.4M1.0k](/packages/laravel-laravel)[malhal/laravel-geographical

Easily add longitude and latitude columns to your records and use inherited functionality for calculating distances

283391.5k](/packages/malhal-laravel-geographical)[unopim/unopim

UnoPim Laravel PIM

10.5k2.2k](/packages/unopim-unopim)[nasirkhan/laravel-starter

A CMS like modular Laravel starter project.

1.4k2.7k](/packages/nasirkhan-laravel-starter)[codewithdennis/larament

Larament is a time-saving starter kit to quickly launch Laravel 13.x projects. It includes FilamentPHP 5.x pre-installed and configured, along with additional tools and features to streamline your development workflow.

3911.7k](/packages/codewithdennis-larament)[ecotone/laravel

Ecotone for Laravel — CQRS, Event Sourcing, Sagas, Durable Workflows, and Outbox on top of Laravel Queue, via PHP attributes.

21313.7k3](/packages/ecotone-laravel)

PHPackages © 2026

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