PHPackages                             touhidurabir/laravel-model-sanitize - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. touhidurabir/laravel-model-sanitize

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

touhidurabir/laravel-model-sanitize
===================================

A laravel package to handle sanitize process of model data to create/update model records.

1.1.2(3y ago)6514.3k↓89.9%3MITPHPPHP &gt;=7.2.0

Since Aug 23Pushed 3y ago2 watchersCompare

[ Source](https://github.com/touhidurabir/laravel-model-sanitize)[ Packagist](https://packagist.org/packages/touhidurabir/laravel-model-sanitize)[ RSS](/packages/touhidurabir-laravel-model-sanitize/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (6)Versions (6)Used By (0)

Laravel Model Sanitize
======================

[](#laravel-model-sanitize)

A simple package to sanitize model data to create/update table records.

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

[](#installation)

Require the package using composer:

```
composer require touhidurabir/laravel-model-sanitize
```

What is does ?
--------------

[](#what-is-does-)

The `Sanitize` package sanitize the passed `attributes` to proper model fillables at create or update.

A model has table schema based multiple attributes associated with it. When we try to create a new model record or update an existing model record, we must provide the an array attributes that is propelry mapped to those arrtibute or table columns names . For example

```
$user = User::create([
    'email' => 'somemail@test.com',
    'password' => Hash::make('password')
]);
```

The above code will run without any issue as both the `email` and `password` column presents in the users table . But for the following code

```
User::create([
    'email' => 'somemail@test.com',
    'password' => 'password',
    'data' => 'some data'
]);
```

It will throw an `\Illuminate\Database\QueryException` if the `data` column not present in the users table.

```
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 table users has no column named data (SQL: insert into "users" ("email", "password", "data", "updated_at", "created_at") values (somemail@mail.com, password, data, 2021-11-14 20:11:04, 2021-11-14 20:11:04))
```

The `Sanitize` package target to make it easier to handle such case as follow by including the `Sanitizable` trait in the models

```
$data = [
    'email' => 'somemail@test.com',
    'password' => 'password',
    'data' => 'some data'
];

User::create($data);
```

The above code will work if the `Sanitizable` trait is used in the `User` model class. it will sanitize the passed attributed to model fillables and table columns, thus removing the `extra or non useable attributes` from it .

How it will be helpful ?
------------------------

[](#how-it-will-be-helpful-)

A great use case of this package is where one need to create multiple model instances from validated request data . For example

```
$validated = $request->validated();

$user = User::create($validated);

$profile = $user->profile->create($validated);
```

I personally use this appraoch in many of my laravel apps .

Usage
-----

[](#usage)

Use the trait `Sanitizable` in model where uuid needed to attach

```
use Touhidurabir\ModelSanitize\Sanitizable;
use Illuminate\Database\Eloquent\Model;

class User extends Model {

    use Sanitizable;
}
```

And thats all . it will automatically work for all the following methods

- **updateOrCreate**
- **firstOrCreate**
- **firstOrNew**
- **create**
- **forceCreate**
- **update**

This package also includes some helper methods that can be used to handle the sanitization process manually.

The `sanitize` static method will sanitize the given attributes list and retuen back the useable and valid attributes as an array

```
$data = [
    'email' => 'somemail@test.com',
    'password' => 'password',
    'data' => 'some data',
    'name' => 'Test User'
];

User::sanitize($data);
```

This will return back as such :

```
[
    'email' => 'somemail@test.com',
    'password' => 'password',
    'name' => 'Test User'
]
```

The `gibberish` static method will sanitize the given attributes list and retuen back the gibberish/non userbale attributes as an array

```
$data = [
    'email' => 'somemail@test.com',
    'password' => 'password',
    'data' => 'some data',
    'name' => 'Test User'
];

User::gibberish($data);
```

This will return back as such :

```
[
    'data' => 'some data',
]
```

The `sanitize` and `gibberish` methods can be used to check or manually sanitize and evaluate the in valid data that can be passed to create/update model records.

It is also possible to `disable/enable` the sanitization process at the runtime using the static methods `disableSanitization` and `enableSanitization` . For example,

```
User::disableSanitization(); // disable the sanitization process
User::enableSanitization();  // enable the sanitization process if previously disabled
```

Contributing
------------

[](#contributing)

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License
-------

[](#license)

[MIT](./LICENSE.md)

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~146 days

Total

5

Last Release

1191d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c6009d764c48df4a55a8645c349f90a99c7e6e694b06c60b580e1da278d1db5?d=identicon)[touhidurabir](/maintainers/touhidurabir)

---

Top Contributors

[![touhidurabir](https://avatars.githubusercontent.com/u/2587979?v=4)](https://github.com/touhidurabir "touhidurabir (5 commits)")[![viicslen](https://avatars.githubusercontent.com/u/39545521?v=4)](https://github.com/viicslen "viicslen (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/touhidurabir-laravel-model-sanitize/health.svg)

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

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

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