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

Overview

Laravel Model UUID

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

Installation

Require the package using composer:

composer require touhidurabir/laravel-model-sanitize

What is does ?

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

A model has multiple table schema based attributed 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' => '[email protected]',
    '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' => '[email protected]', 
    '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 with message 'SQLSTATE[HY000] [2002] Connection refused (SQL: insert into `users` (`email`, `password`, `updated_at`, `created_at`) values ([email protected], password, 2021-08-23 10:15:25, 2021-08-23 10:15:25))'

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

$data = [
    'email' => '[email protected]', 
    '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 ?

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

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' => '[email protected]', 
    'password' => 'password', 
    'data' => 'some data', 
    'name' => 'Test User'
];

User::sanitize($data);

This will return back as such :

[
    'email' => '[email protected]', 
    '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' => '[email protected]', 
    '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.

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

MIT

You might also like...
Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard
Laravel Seo package for Content writer/admin/web master who do not know programming but want to edit/update SEO tags from dashboard

Laravel Seo Tools Laravel is becoming more and more popular and lots of web application are developing. In most of the web application there need some

A package to handle the SEO in any Laravel application, big or small.
A package to handle the SEO in any Laravel application, big or small.

Never worry about SEO in Laravel again! Currently there aren't that many SEO-packages for Laravel and the available ones are quite complex to set up a

Laravel Soulbscription - This package provides a straightforward interface to handle subscriptions and features consumption.

About This package provides a straightforward interface to handle subscriptions and features consumption. Installation You can

Simple package to handle response properly in your API.

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.

A package for simplifying the integration of a maker-checker approval process to your Laravel application.

prismaticoder/maker-checker-laravel The prismaticoder/maker-checker-laravel package is a comprehensive Laravel package that provides a flexible and cu

A laravel package to generate model hashid based on model id column.

Laravel Model Hashid A package to generate model hash id from the model auto increment id for laravel models Installation Require the package using co

A package to filter laravel model based on query params or retrieved model collection

Laravel Filterable A package to filter laravel model based on query params or retrived model collection. Installation Require/Install the package usin

Laravel package to create autonumber for Eloquent model

Laravel AutoNumber Laravel package to create autonumber for Eloquent model Installation You can install the package via composer: composer require gid

Save Model is a Laravel package that allows you to save data in the database in a new way.

Save Model is a Laravel package that allows you to save data in the database in a new way. No need to worry about $guarded and $fillable properties in the model anymore. Just relax an use Save Model package.

Comments
  • Will it work with multidimensional array?

    Will it work with multidimensional array?

    As the title said, I tried to create from this data :

    $data = [
        [ 'name' => 'someone', 'related_item' => 2, 'pickby' => 3],
        [ 'name' => 'another one', 'related_item' => 1, 'pickby' => 4]
    ];
    

    I tried it with create method but I check the query it all gone. So, is this library works with multidimensional array, or not? if it's supposed to work, could you help me why my data is gone before insert? thanks!

    opened by fahmiegerton 1
  • Set schema builder connection

    Set schema builder connection

    Set schema builder connection to the same connection as the model when sanitizing the data.

    This makes the package compatible with multiple multi-tenancy packages which change the database connection dynamically.

    opened by viicslen 0
Releases(1.1.1)
Owner
null
webtrees module: enhanced clippings cart with more functions to add records to the clippings cart and to start actions on these records

webtrees module hh_clippings_cart_enhanced !!! This is an alpha version! Do not use it in a productive webtrees system! !!! This webtrees custom modul

Hermann Hartenthaler 1 Sep 18, 2022
A simple laravel package to handle multiple key based model route binding

Laravel Model UUID A simple package to handle the multiple key/column based route model binding for laravel package Installation Require the package u

null 13 Mar 2, 2022
A laravel package to handle cascade delete and restore on model relations.

Laravel Model Soft Cascade A laravel package to handle cascade delete and restore on model relations. This package not only handle the cascade delete

Touhidur Rahman 18 Apr 29, 2022
A laravel package to handle model specific additional meta fields in an elegant way.

Laravel Meta Fields A php package for laravel framework to handle model meta data in a elegant way. Installation Require the package using composer: c

Touhidur Rahman 26 Apr 5, 2022
An open source Laravel Soundboard with Admin Panel CRUD (Create Read Update Delete) built on Laravel, Bootstrap, and Vue.js

Laravel Soundboard An open source Laravel Soundboard with Admin Panel CRUD (Create Read Update Delete) built on Laravel 5.8, Bootstrap 4, Vue.js, Boot

Jeremy Kenedy 24 Oct 28, 2022
Html-sanitizer - The HtmlSanitizer component provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a document's DOM.

HtmlSanitizer Component The HtmlSanitizer component provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a documen

Symfony 201 Dec 23, 2022
Allow your model to record the creation, update and deletion of user fingerprints in laravel packages

This laravel package will allow your models to record the the created, updated and deleted by User FingerPrints

Managemize 4 Mar 11, 2022
A simple laravel state machine to handle model transitions, based on a pre-defined list of rules

A simple state machine that allows transitioning model states based on pre-defined rules. Installation You can install the package via composer: compo

Jack Mollart 18 Apr 2, 2022
A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

A Laravel wrapper for spatie/dns. Allows to query and validate DNS records.

Astrotomic 22 Nov 17, 2022
Effortlessly streamline tables and records printing in PDF/XLSX in your FilamentPHP application.

Filament Printables: a package to generate reports and form printables for your app. This is a work in progress thing Installation You can install the

fastOFI Corp 6 Jun 15, 2023