PHPackages                             theriddleofenigma/laravel-model-validation - 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. theriddleofenigma/laravel-model-validation

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

theriddleofenigma/laravel-model-validation
==========================================

Model validation for Laravel application.

v1.6.0(11mo ago)348.4k↓15.8%5[1 issues](https://github.com/theriddleofenigma/laravel-model-validation/issues)9MITPHPCI failing

Since Nov 17Pushed 11mo ago2 watchersCompare

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

READMEChangelog (7)Dependencies (1)Versions (9)Used By (9)

`♥ Made with  And I love `

[![](https://camo.githubusercontent.com/9755c0ffed17a24ecdf1205f06baf32fd5acfaf7a998d6ebe783b348ebb600f6/68747470733a2f2f6170702e666f7373612e636f6d2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d736869656c64)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield)

Laravel Model Validation
========================

[](#laravel-model-validation)

[![FOSSA Status](https://camo.githubusercontent.com/ad7b2d5a6038706bfed573f2fb6cd421f949839fd050bc5ea1d068bb4c847067/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d736869656c64)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield)

Model validation - Validates the model data. \*Only for laravel applications.

An easy validator option for your eloquent models. Also have flexibility for additional codes that might be executed on before and after validation.

### Composer install

[](#composer-install)

```
composer require theriddleofenigma/laravel-model-validation
```

Usage Examples
--------------

[](#usage-examples)

Here user model is mentioned as an example. You could use this in any model you want.

### User.php model

[](#userphp-model)

```
use Enigma\ValidatorTrait;

class User extends Model
{
    use ValidatorTrait;

    /**
     * Boot method.
     */
    public static function boot()
    {
        parent::boot();

        // Add this method for validating the current model on model saving event
        static::validateOnSaving();
    }

    public $validationRules = [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];

    public $validationMessages = [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];

    public $validationAttributes = [
        'name' => 'User Name'
    ];

    /**
     * Code to be executed before the validation goes here.
     */
    public function beforeValidation()
    {
        // Some code goes here..
    }

    /**
     * Code to be executed after the validation goes here.
     */
    public function afterValidation()
    {
        // Some code goes here..
    }
}
```

### Control the data get validated

[](#control-the-data-get-validated)

You can control the data which gets validated by adding validationData method.

```
/**
 * Validation data to be validated.
 *
 * @return array
 */
public function validationData(array $data)
{
    // Here $data is the value of $this->getAttributes(), feel free to use your own code to produce the data. Ex: $this->toArray(), $this->getOriginal(), etc.,
    $data["name"] = strtolower($data["name"]);

    // Note: This wouldn't affect your actual model data which is going to persist in DB.

    return $data;
}
```

### Other options

[](#other-options)

You could mention the validation rules, attributes and messages as a property as well as method.

```
/**
 * Validation rules to validate.
 *
 * @return array
 */
public function validationRules()
{
    // You can process your code here and return the rules as however you want.
    return [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];
}

/**
 * Custom messages to replace the validation messages.
 *
 * @return array
 */
public function validationMessages()
{
    // You can process your code here and return the messages as however you want.
    return [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];
}

/**
 * Custom attribute names to replace the validation attribute name.
 *
 * @return array
 */
public function validationAttributes()
{
    return [
        'name' => 'User Name'
    ];
}
```

You could mention the validation only for creating itself or on any model event just add `$model->validate()`.

```
/**
 * Boot method.
 */
public static function boot()
{
    parent::boot();

    // You can mention like this for validating the model on custom events as your wish
    self::creating(function($model){
        $model->validate();
    });

    // Or you can make use of the alias `self::validateOnCreating()`.
}
```

Refer the available methods in the ValidationTrait.

License
-------

[](#license)

Laravel Model Validation is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

[![FOSSA Status](https://camo.githubusercontent.com/c084773db5b14f657c72d8f6d7979174226321d0c8596c0274a32d6da2524c14/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d6c61726765)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_large)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance51

Moderate activity, may be stable

Popularity37

Limited adoption so far

Community25

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 86.5% 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 ~342 days

Recently: every ~374 days

Total

8

Last Release

345d ago

Major Versions

0.1.7 → v1.02020-10-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/78fdf9bceef9ac86b4cb041802ade60afec5fc1235cf97cc582deac2ce2dadb8?d=identicon)[theriddleofenigma](/maintainers/theriddleofenigma)

---

Top Contributors

[![theriddleofenigma](https://avatars.githubusercontent.com/u/29883026?v=4)](https://github.com/theriddleofenigma "theriddleofenigma (64 commits)")[![kumaravel011](https://avatars.githubusercontent.com/u/30690409?v=4)](https://github.com/kumaravel011 "kumaravel011 (6 commits)")[![fossabot](https://avatars.githubusercontent.com/u/29791463?v=4)](https://github.com/fossabot "fossabot (1 commits)")[![ksraylan](https://avatars.githubusercontent.com/u/32464286?v=4)](https://github.com/ksraylan "ksraylan (1 commits)")[![rubenmuehlhans](https://avatars.githubusercontent.com/u/85454114?v=4)](https://github.com/rubenmuehlhans "rubenmuehlhans (1 commits)")[![syehan](https://avatars.githubusercontent.com/u/21031766?v=4)](https://github.com/syehan "syehan (1 commits)")

---

Tags

databaseeloquenteloquent-modelslaravellaravel-applicationlaravel-frameworkmodelmodel-validationphpvalidation

### Embed Badge

![Health badge](/badges/theriddleofenigma-laravel-model-validation/health.svg)

```
[![Health](https://phpackages.com/badges/theriddleofenigma-laravel-model-validation/health.svg)](https://phpackages.com/packages/theriddleofenigma-laravel-model-validation)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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