PHPackages                             olsgreen/laravel-guardian - 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. olsgreen/laravel-guardian

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

olsgreen/laravel-guardian
=========================

A simple base class for self validating models for Laravel 4.x and Eloquent.

0240PHP

Since Oct 6Pushed 11y ago1 watchersCompare

[ Source](https://github.com/olsgreen/laravel-guardian)[ Packagist](https://packagist.org/packages/olsgreen/laravel-guardian)[ RSS](/packages/olsgreen-laravel-guardian/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Guardian
========

[](#guardian)

Self validating models for smarter &amp; cleaner Laravel 4 Eloquent ORM. Inspired by [Ardent](https://github.com/laravelbook/ardent).

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

[](#installation)

Add our repository to your `composer.json` file like so:

```
"repositories": [{
        "type": "vcs",
        "url": "https://github.com/olsgreen/Guardian"
    }],

```

also require `Guardian`:

```
"require": {
	"olsgreen/guardian": "dev-master"
},

```

then run `composer update`.

Getting started
---------------

[](#getting-started)

Guardian extends the Eloquent model making it extremely easy to retrofit models or remove later if needed.

To get up and running all you need to do is change your model to extend from `Guardian` and add a `getValidator()` method so that `Guardian` can obtain a correctly configured instance of a `Validator`.

```
use Olsgreen\Guardian\Guardian;

class User extends Guardian {

	........

	/**
	 * Overridden from the base class
	 *
	 * @return Illuminate\Validation\Validator
	 */
	public function getValidator()
	{
		// Standard rules
		$rules = array(
			'first_name' => 'required|min:3',
			'last_name' => 'required|min:3',
			'username' => 'required|unique:users,username|min:2',
			'password' => 'required|min:6',
			'email' => 'email|unique:users,email',
			);

		// Rules for models that already exist
		if ($this->exists) {
			$rules['username'] = 'required|unique:users,username,' . $this->id . '|min:2';
			$rules['email'] = 'email|unique:users,email,' . $this->id;
		}

		// Return the validator
		return Validator::make($this->attributes, $rules);
	}

	........

}

```

Validation Errors
-----------------

[](#validation-errors)

Guardian will automatically validate the model on calling `save()`. If validation fails `save()` will `return false` otherwise it will `return true`, providing the underlying save was successful.

If the validation fails your can get the `MessageBag` containing the validation errors by calling `getValidationMessages()` on the model.

An example of saving a user within a controller:

```
if (!$user->save()) {
	return Redirect::route('user.form')->withErrors($user->getValidationMessages());
}

```

Force Save
----------

[](#force-save)

You can force a save, completely bypassing validation, by calling `forceSave()` on a model.

Sidestep MassAssignmentExceptions with fillFillable()
-----------------------------------------------------

[](#sidestep-massassignmentexceptions-with-fillfillable)

`fillFillable()` allows you to quickly and easily fill a model with an array of attributes. This method conveintly sidesteps the `MassAssignmentException` generated when trying to use the `fill()` method with `Input::all()`. A usage example is below:

```
$user = User::find(1);
$user->fillFillable(Input::all());
$user->save();

```

License
-------

[](#license)

Copyright (c) 2014 Oliver Green

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

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

---

Top Contributors

[![olsgreen](https://avatars.githubusercontent.com/u/1324164?v=4)](https://github.com/olsgreen "olsgreen (8 commits)")

### Embed Badge

![Health badge](/badges/olsgreen-laravel-guardian/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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