PHPackages                             sandaffo/laravel-model-validator - 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. sandaffo/laravel-model-validator

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

sandaffo/laravel-model-validator
================================

A Laravel package to add validation methods directly to Eloquent models

v1.0.4(1y ago)011PHPPHP ^8.0

Since Jul 23Pushed 1y ago2 watchersCompare

[ Source](https://github.com/sandeep-daffodilsw/laravel-model-validator)[ Packagist](https://packagist.org/packages/sandaffo/laravel-model-validator)[ RSS](/packages/sandaffo-laravel-model-validator/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

Laravel Model Validator
=======================

[](#laravel-model-validator)

A Laravel package to add validation rules and messages at the model level.

After install this package, you don't need to write the validation rules inside controller or form request, you can add them inside model and directly use methods to validate.

Adding validation rules inside model itself makes model readable for developers.

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

[](#installation)

Install the package via composer:

```
composer require sandaffo/laravel-model-validator
```

Usage
-----

[](#usage)

### Adding Validation to a Model

[](#adding-validation-to-a-model)

To add validation to a model, define the `rules` and `messages` properties in your model:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // define rules array
    public static $rules = [
        'title' => 'required|string|max:255',
        'slug' => 'required|string|max:255|unique:posts',
        'description' => 'required|string|min:8',
    ];

    // messages are optional, only provide when want customize message
    public static $messages = [
        'title.required' => 'The title field is required.',
        'slug.required' => 'The slug field is required.',
        'description.required' => 'The description field is required.',
    ];
}
```

### Example

[](#example)

Below is an example demonstrating the usage of the package:

```
$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

if ($p->isValid()) { // $p->isValid() returns true or false
    echo "Post is valid";
    $p->save();
} else {
    echo "Post is not valid";
    print_r($p->errors());
}
```

### Tinker Example

[](#tinker-example)

```
$ php artisan tinker
```

```
$p = new Post();
$p->title = "Test Post One";
$p->slug = "test-post-one";
$p->description = "Test post one description";

$p->isValid(); // true or false
$p->errors(); // []

$p->errorMessages(); // [] single dimensional array of all error messages

$p->getRules(); // to get the defined rules if you need somewhere
// [
//     "title" => "required|string|max:255",
//     "slug" => "required|string|max:255|unique:posts",
//     "description" => "required|string|min:8",
// ]

$p->getMessages(); // [] of defined custom error messages

$p->getValidator(); // return Validator object if you need
// Illuminate\Validation\Validator { ... }

$p->getValidator()->errors();
// Illuminate\Support\MessageBag { ... }

$p->save(); // true

$p;
// App\Models\Post {
//     title: "Test Post One",
//     slug: "test-post-one",
//     description: "Test post one description",
//     updated_at: "2024-07-24 08:45:45",
//     created_at: "2024-07-24 08:45:45",
//     id: 1,
// }
```

Methods
-------

[](#methods)

- `isValid()`: Checks if the model is valid according to the defined rules.
- `errors()`: Returns an array of validation errors.
- `errorMessages()`: Returns an array of error messages.
- `getRules()`: Returns the validation rules.
- `getMessages()`: Returns the validation messages.
- `getValidator()`: Returns the validator instance.

License
-------

[](#license)

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

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

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

Total

5

Last Release

710d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16536986?v=4)[sandeep-daffodilsw](/maintainers/sandeep-daffodilsw)[@sandeep-daffodilsw](https://github.com/sandeep-daffodilsw)

---

Top Contributors

[![sandeep-daffodilsw](https://avatars.githubusercontent.com/u/16536986?v=4)](https://github.com/sandeep-daffodilsw "sandeep-daffodilsw (5 commits)")

---

Tags

databaseeloquenteloquent-modelslaravellaravel-frameworklaravel-model-validatormodelmodel-validationphpsandaffosandaffo-laravel-model-validatorsandeep-daffodilsandeep-daffodilswvalidation

### Embed Badge

![Health badge](/badges/sandaffo-laravel-model-validator/health.svg)

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

###  Alternatives

[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[lemaur/eloquent-publishing

218.1k1](/packages/lemaur-eloquent-publishing)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

782.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

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