PHPackages                             magniloquent/magniloquent - 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. magniloquent/magniloquent

Abandoned → [dwightwatson/validating](/?search=dwightwatson%2Fvalidating)ArchivedLibrary[Database &amp; ORM](/categories/database)

magniloquent/magniloquent
=========================

Self-validating models for Laravel 4's Eloquent

708.2k7[3 issues](https://github.com/philipbrown/magniloquent/issues)[1 PRs](https://github.com/philipbrown/magniloquent/pulls)PHP

Since Aug 11Pushed 11y ago1 watchersCompare

[ Source](https://github.com/philipbrown/magniloquent)[ Packagist](https://packagist.org/packages/magniloquent/magniloquent)[ RSS](/packages/magniloquent-magniloquent/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Magniloquent (DEPRECATED)
=========================

[](#magniloquent-deprecated)

Self-validating models for Laravel 4's Eloquent.

Based on the excellent [Ardent](https://github.com/laravelbook/ardent) package by [Max Ehsan](https://github.com/laravelbook).

This package is highly inspired by Ardent. I wanted to make some big changes and so I thought it would be better to start a new package rather than fundamentally change how Ardent validates data. If you are looking to extend Eloquent's functionality, you should also check out Ardent!

Magniloquent was extracted from [Cribbb](https://github.com/cribbb/cribbb).

\##Installation Add `magniloquent/magniloquent` as a requirement to `composer.json`:

```
{
  "require": {
    "magniloquent/magniloquent": "dev-master"
  }
}
```

Update your packages with `composer update` or install with `composer install`.

\##Getting Started Magniloquent extends Eloquent rather than replaces it, and so to use Magniloquent, you need to extend your models like this:

```
use Magniloquent\Magniloquent\Magniloquent;

class User extends Magniloquent{}
```

All of Eloquent's functionality is still available so you can continue to interact with your models as you normally would. If one of your models does not require validation, you don't have to use Magniloquent, you are free to mix and match.

\##Validation Rules For each model, you need to set validation rules that control what type of data can be inserted into the database. Generally you are free to do this wherever you want, but to use Magniloquent you should keep your rules inside the model.

Magniloquent uses Laravel's excellent [Validation](http://laravel.com/docs/validation) class so you defining your rules is really easy.

Your validation rules are simply stored as a static parameter and are seperated into `save`, `create` and `update` arrays:

```
/**
 * Validation rules
 */
public static $rules = array(
  "save" => array(
    'username' => 'required|min:4',
    'email'    => 'required|email',
    'password' => 'required|min:8'
  ),
  "create" => array(
    'username'              => 'unique:users',
    'email'                 => 'unique:users',
    'password'              => 'confirmed',
    'password_confirmation' => 'required|min:8'
  ),
  "update" => array()
);
```

The `save` array are validation rules that are applicable whenever the model is changed. The `create` and `update` arrays are only added on their respective methods.

So in the example above, when a user is created, the username should be unique. When the user updates any of their information, the uniqueness validation test won't be applied.

Note: Magniloquent is able to correctly ignore the current object when validatings unique values.

\##Easier Relationships Defining relationships in Laravel can take up a ton of room in a model. This can make reading and maintaining your models much more difficult. Luckily, Magniloquent makes defining relationships a cinch. Add a `$relationships` multi-dimensional array to your model. Inside it, define the name of the relationship that will be called as the key and the value to be an array of parameters. The first parameter is the type of relationship. The rest are the parameters to be passed to that function. Below is an example:

```
class Athlete extends Magniloquent {

    protected static $relationships = array(
        'trophies' => array('hasMany', 'Trophy'),
        'team'     => array('belongsTo', 'Team', 'team_id'),
        'sports'   => array('belongsToMany', 'Sport', 'athletes_sports', 'athlete_id', 'sport_id')
    );

}
```

\##Custom Purging Magniloquent will automatically purge any attributes that start with an underscore `_` or end with `_confirmation`. If you want to purge additional fields, add a `protected static $purgeable` array whose keys are the attributes to purge. Below is an example:

```
class Account extends Magniloquent {

    protected static $purgeable = ['ssn'];

}
```

Anytime this model is saved, the `$ssn` attribute will be removed from the object before it is saved. This allows you to run code the code below without worrying about inserting unnecessary data into the database.

```
$account->save(Input::all());
```

\##Custom Display Names Magniloquent gives you the ability to customize the display name of each of the fields that are under validation. Just add a niceNames() class method returning an array where the keys are the field names and the values are their display names. Below is an example.

```
protected function niceNames()
{
    return array(
        'email'     => 'email address'
    );
}
```

Now, anytime there are issues with email validation, the message to the user will say "email address" instead of "email". Optionally you could also make use of trans() or some custom logic code for returning localized or computed display names.

Note: in older versions this feature was implemented as the $nicenames class property. Legacy support for this is preserved but you are encouraged to use niceNames() in new models.

\##Controller Example Here is an example `store` method:

```
/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
  $s = User::create(Input::all());

  if($s->isSaved())
  {
    return Redirect::route('users.index')
      ->with('flash', 'The new user has been created');
  }

  return Redirect::route('users.create')
    ->withInput()
    ->withErrors($s->errors());
}
```

**First** Use Laravel's create method and send in the `Input::all()`. Save the return value into a variable.

**Second** Determine whether the model saved corrected using the `saved()` method.

**Third** Return the validation errors using the `errors()` method.

The returned errors use Laravel's [MessageBag](http://laravel.com/docs/validation#error-messages-and-views).

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2014 Philip Brown and Alex Sears

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

30

—

LowBetter than 62% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 79.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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1579059?v=4)[Philip Brown](/maintainers/philipbrown)[@philipbrown](https://github.com/philipbrown)

---

Top Contributors

[![philipbrown](https://avatars.githubusercontent.com/u/1579059?v=4)](https://github.com/philipbrown "philipbrown (54 commits)")[![RicardoRamirezR](https://avatars.githubusercontent.com/u/6526545?v=4)](https://github.com/RicardoRamirezR "RicardoRamirezR (3 commits)")[![Propaganistas](https://avatars.githubusercontent.com/u/6680176?v=4)](https://github.com/Propaganistas "Propaganistas (3 commits)")[![gregoryhugaerts](https://avatars.githubusercontent.com/u/40509980?v=4)](https://github.com/gregoryhugaerts "gregoryhugaerts (2 commits)")[![pbcobweb](https://avatars.githubusercontent.com/u/7373739?v=4)](https://github.com/pbcobweb "pbcobweb (1 commits)")[![lenrsmith](https://avatars.githubusercontent.com/u/3423353?v=4)](https://github.com/lenrsmith "lenrsmith (1 commits)")[![kennonb](https://avatars.githubusercontent.com/u/76159?v=4)](https://github.com/kennonb "kennonb (1 commits)")[![rabas](https://avatars.githubusercontent.com/u/1055527?v=4)](https://github.com/rabas "rabas (1 commits)")[![jfexyz](https://avatars.githubusercontent.com/u/66879?v=4)](https://github.com/jfexyz "jfexyz (1 commits)")[![TheBox193](https://avatars.githubusercontent.com/u/220755?v=4)](https://github.com/TheBox193 "TheBox193 (1 commits)")

### Embed Badge

![Health badge](/badges/magniloquent-magniloquent/health.svg)

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

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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