Skip to content

Extends Laravel's HasAttributes Trait to enable nested attribute mutators for JSON cast columns

License

Notifications You must be signed in to change notification settings

nullthoughts/laravel-json-column-mutators-trait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

laravel-json-column-mutators-trait

Extends Laravel's HasAttributes Trait to enable nested attribute Mutators for JSON cast columns. Out of the box, Laravel does not support Mutators on JSON columns.

Usage

  1. Copy into App\Traits, and declare use SetJsonMutator; within the Model that is using JSON columns
<?php

namespace App;

use App\Traits\SetJsonMutator;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use SetJsonMutator;
}
  1. Create a new Mutator in your Model, example:
setPricingMsrpAttribute($value)
{
  return $value * 2;
}
  1. Update a record using the JSON -> syntax:
$product = Product::first();

$product->update([
    'pricing->msrp' => 150,
]);

return $product->pricing['msrp']; //300

About

Extends Laravel's HasAttributes Trait to enable nested attribute mutators for JSON cast columns

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages