PHPackages                             zschuessler/model-json-attribute-guard-laravel - 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. zschuessler/model-json-attribute-guard-laravel

ActiveLibrary

zschuessler/model-json-attribute-guard-laravel
==============================================

An intuitive and fun way to bring consistency to your json columns

1.0.1(5y ago)16121LGPL-2.1-onlyPHPPHP &gt;=7.2CI failing

Since Aug 27Pushed 5y ago1 watchersCompare

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

READMEChangelog (2)Dependencies (4)Versions (2)Used By (0)

JSON Column Guard
=================

[](#json-column-guard)

This package allows validation and custom casts for JSON columns.

Have you ever tried to use a json column, and:

1. Validating schema of the column was difficult or messy
2. Querying the column was onerous
3. Casting types when saving models wasn't fun

Well now you can rest easy!

Example
-------

[](#example)

You want to save user preferences. It's highly dynamic data, so you throw it into a json column:

 Database Table Database Column Desired Schemauserspreferences```
[
  {
    "name": "Favorite Band",
    "value": "Slenderbodies",
    "date_created": "2019-09-15",
    "date_updated": "2020-01-01"
    }
]
```

Here are problems:

1. Can you enforce each key is valid?
2. Can you enforce the two dates are valid dates?
3. What about always ensuring the column is an array, regardless if a preference exists or not?

*We can.*

Step 1: Create The Validator Class
----------------------------------

[](#step-1-create-the-validator-class)

Let's use Laravel's own Validator syntax to describe what we want.

```
