diff --git a/src/Auth/Models/Preferences.php b/src/Auth/Models/Preferences.php index ff109c2a7..6e895206d 100644 --- a/src/Auth/Models/Preferences.php +++ b/src/Auth/Models/Preferences.php @@ -27,9 +27,11 @@ class Preferences extends Model protected static $cache = []; /** - * @var array jsonable attribute names that are json encoded and decoded from the database + * @var array casts attribute types to convert automatically */ - protected $jsonable = ['value']; + protected $casts = [ + 'value' => 'array' + ]; /** * @var \October\Rain\Auth\Models\User userContext is the user that owns the preferences diff --git a/src/Auth/Models/Role.php b/src/Auth/Models/Role.php index fb73d1f00..04902a744 100644 --- a/src/Auth/Models/Role.php +++ b/src/Auth/Models/Role.php @@ -30,9 +30,11 @@ class Role extends Model ]; /** - * @var array jsonable attribute names that are json encoded and decoded from the database + * @var array casts attribute types to convert automatically */ - protected $jsonable = ['permissions']; + protected $casts = [ + 'permissions' => 'array' + ]; /** * @var array allowedPermissionsValues diff --git a/src/Auth/Models/User.php b/src/Auth/Models/User.php index 2b350aae0..c974ff313 100644 --- a/src/Auth/Models/User.php +++ b/src/Auth/Models/User.php @@ -87,9 +87,11 @@ class User extends Model implements Authenticatable public $customMessages = []; /** - * @var array jsonable attribute names that are json encoded and decoded from the database + * @var array casts attribute types to convert automatically */ - protected $jsonable = ['permissions']; + protected $casts = [ + 'permissions' => 'array' + ]; /** * allowedPermissionsValues diff --git a/src/Database/Concerns/HasJsonable.php b/src/Database/Concerns/HasJsonable.php deleted file mode 100644 index 25f7b5bcf..000000000 --- a/src/Database/Concerns/HasJsonable.php +++ /dev/null @@ -1,89 +0,0 @@ -jsonable = array_merge($this->jsonable, $attributes); - } - - /** - * isJsonable checks if an attribute is jsonable or not. - * - * @return array - */ - public function isJsonable($key) - { - return in_array($key, $this->jsonable); - } - - /** - * getJsonable attributes name - * - * @return array - */ - public function getJsonable() - { - return $this->jsonable; - } - - /** - * jsonable attributes set for the model. - * - * @param array $jsonable - * @return $this - */ - public function jsonable(array $jsonable) - { - $this->jsonable = $jsonable; - - return $this; - } - - /** - * addJsonableAttributesToArray - * @return array - */ - protected function addJsonableAttributesToArray(array $attributes, array $mutatedAttributes) - { - foreach ($this->jsonable as $key) { - if ( - !array_key_exists($key, $attributes) || - in_array($key, $mutatedAttributes) - ) { - continue; - } - - // Prevent double decoding of jsonable attributes. - if (!is_string($attributes[$key])) { - continue; - } - - $jsonValue = json_decode($attributes[$key], true); - if (json_last_error() === JSON_ERROR_NONE) { - $attributes[$key] = $jsonValue; - } - } - - return $attributes; - } -} diff --git a/src/Database/ExpandoModel.php b/src/Database/ExpandoModel.php index 2b51ce892..eccb416b9 100644 --- a/src/Database/ExpandoModel.php +++ b/src/Database/ExpandoModel.php @@ -33,7 +33,7 @@ public function __construct(array $attributes = []) // Process attributes last for traits with attribute modifiers $this->bindEvent('model.beforeSaveDone', [$this, 'expandoBeforeSaveDone'], -1); - $this->addJsonable($this->expandoColumn); + $this->addCasts([$this->expandoColumn => 'array']); } /** diff --git a/src/Database/Model.php b/src/Database/Model.php index 295727381..081f4bce1 100644 --- a/src/Database/Model.php +++ b/src/Database/Model.php @@ -19,7 +19,6 @@ class Model extends EloquentModel { use Concerns\HasEvents; - use Concerns\HasJsonable; use Concerns\HasAttributes; use Concerns\HasReplication; use Concerns\HasRelationships; diff --git a/src/Database/Models/DeferredBinding.php b/src/Database/Models/DeferredBinding.php index 1fbc2500b..a094616c2 100644 --- a/src/Database/Models/DeferredBinding.php +++ b/src/Database/Models/DeferredBinding.php @@ -21,9 +21,11 @@ class DeferredBinding extends Model public $table = 'deferred_bindings'; /** - * @var array jsonable attribute names that are json encoded and decoded from the database + * @var array casts attribute types to convert automatically */ - protected $jsonable = ['pivot_data']; + protected $casts = [ + 'pivot_data' => 'array' + ]; /** * @var array nullable attribute names which should be set to null when empty diff --git a/src/Database/Traits/Validation.php b/src/Database/Traits/Validation.php index 08169e53a..bad7dd4ef 100644 --- a/src/Database/Traits/Validation.php +++ b/src/Database/Traits/Validation.php @@ -337,11 +337,6 @@ public function validate($rules = null, $customMessages = null, $attributeNames if (!empty($rules)) { $data = $this->getValidationAttributes(); - // Decode jsonable attribute values - foreach ($this->getJsonable() as $jsonable) { - $data[$jsonable] = $this->getAttribute($jsonable); - } - // Add relation values, if specified. foreach ($rules as $attribute => $rule) { if (!$this->hasRelation($attribute) || array_key_exists($attribute, $data)) { diff --git a/src/Scaffold/Console/contentfield/contentfield.stub b/src/Scaffold/Console/contentfield/contentfield.stub index c8e6320c3..41d651bc1 100644 --- a/src/Scaffold/Console/contentfield/contentfield.stub +++ b/src/Scaffold/Console/contentfield/contentfield.stub @@ -41,7 +41,7 @@ class {{studly_name}} extends ContentFieldBase */ public function extendModelObject($model) { - $model->addJsonable($this->fieldName); + $model->addCasts([$this->fieldName => 'array']); } /**