From 7b2018a934005f506ea49c5a86282e2b3809809d Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Wed, 6 Oct 2021 13:56:39 +0100 Subject: [PATCH 1/3] refactor: update schemaless attributes --- .../Eloquent/Concerns/HasSchemalessAttributes.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php b/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php index e51f38031..228a3dfad 100644 --- a/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php +++ b/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php @@ -6,18 +6,12 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; -use Spatie\SchemalessAttributes\SchemalessAttributes; trait HasSchemalessAttributes { - public function getExtraAttributesAttribute(): SchemalessAttributes - { - return SchemalessAttributes::createForModel($this, 'extra_attributes'); - } - public function scopeWithExtraAttributes(): Builder { - return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes'); + return $this->extra_attributes->modelScope(); } public function getMetaAttribute($name, $default = null) From 8ca33419aa6d7ea9ffa34bd6391740a1997962cc Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Wed, 6 Oct 2021 13:56:59 +0100 Subject: [PATCH 2/3] add usage example --- examples/ui.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/examples/ui.md b/examples/ui.md index 20bb1e7e4..cbf9e77d6 100644 --- a/examples/ui.md +++ b/examples/ui.md @@ -1,4 +1,24 @@ -# Simple Examples +# Development + +## Schemaless Attributes + +In order to use the `HasSchemalessAttributes` trait, you will need to ensure the model has the correct casting applied: + +```php + SchemalessAttributes::class, + ]; +} +``` + +# Component Examples This file contains basic examples and explains the parameters that can be used for the components. From 59050a3e24f1c79fb4925fb789c0a8eed92c0f3a Mon Sep 17 00:00:00 2001 From: Alex Barnsley <8069294+alexbarnsley@users.noreply.github.com> Date: Wed, 6 Oct 2021 14:04:01 +0100 Subject: [PATCH 3/3] update methods to match nodem's --- .../Concerns/HasSchemalessAttributes.php | 68 ++++++++++++++++--- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php b/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php index 228a3dfad..e699cdadf 100644 --- a/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php +++ b/src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php @@ -9,44 +9,94 @@ trait HasSchemalessAttributes { + /** + * Scope the query to include the meta attributes. + * + * @return \Illuminate\Database\Eloquent\Builder + */ public function scopeWithExtraAttributes(): Builder { return $this->extra_attributes->modelScope(); } - public function getMetaAttribute($name, $default = null) + /** + * Get the meta attribute for the model. + * + * @param string|array|null $name + * @param mixed $default + * + * @return mixed + */ + public function getMetaAttribute(string | array | null $name, $default = null) { return $this->extra_attributes->get($name, $default); } - public function setMetaAttribute($name, $value) + /** + * Set the meta attribute for the model. + * + * @param string|array $name + * @param mixed $value + * + * @return self + */ + public function setMetaAttribute(string | array $name, $value): self { $this->extra_attributes->set($name, $value); - $this->save(); + return tap($this)->save(); } - public function hasMetaAttribute($name) + /** + * Determine whether the model contains a given meta attribute. + * + * @param string $name + * + * @return bool + */ + public function hasMetaAttribute(string $name): bool { - return ! empty($this->extra_attributes->get($name)); + return $this->extra_attributes->get($name) !== null; } - public function forgetMetaAttribute($name) + /** + * Remove the meta attribute for the model. + * + * @param string $name + * + * @return self + */ + public function forgetMetaAttribute(string $name): self { $this->extra_attributes->forget($name); - $this->save(); + return tap($this)->save(); } - public function fillMetaAttributes($attributes) + /** + * Fill the model's meta attributes. + * + * @param array $attributes + * + * @return self + */ + public function fillMetaAttributes(array $attributes): self { foreach ($attributes as $name => $value) { $this->extra_attributes->set($name, $value); } - $this->save(); + return tap($this)->save(); } + /** + * Update or create model with the schemaless attributes. + * + * @param array $attributes + * @param array $values + * + * @return self + */ public static function updateOrCreateWithMeta(array $attributes, array $values): Model { $model = static::withExtraAttributes($attributes)->first();