|
6 | 6 |
|
7 | 7 | use Illuminate\Database\Eloquent\Builder; |
8 | 8 | use Illuminate\Database\Eloquent\Model; |
9 | | -use Spatie\SchemalessAttributes\SchemalessAttributes; |
10 | 9 |
|
11 | 10 | trait HasSchemalessAttributes |
12 | 11 | { |
13 | | - public function getExtraAttributesAttribute(): SchemalessAttributes |
14 | | - { |
15 | | - return SchemalessAttributes::createForModel($this, 'extra_attributes'); |
16 | | - } |
17 | | - |
| 12 | + /** |
| 13 | + * Scope the query to include the meta attributes. |
| 14 | + * |
| 15 | + * @return \Illuminate\Database\Eloquent\Builder |
| 16 | + */ |
18 | 17 | public function scopeWithExtraAttributes(): Builder |
19 | 18 | { |
20 | | - return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes'); |
| 19 | + return $this->extra_attributes->modelScope(); |
21 | 20 | } |
22 | 21 |
|
23 | | - public function getMetaAttribute($name, $default = null) |
| 22 | + /** |
| 23 | + * Get the meta attribute for the model. |
| 24 | + * |
| 25 | + * @param string|array|null $name |
| 26 | + * @param mixed $default |
| 27 | + * |
| 28 | + * @return mixed |
| 29 | + */ |
| 30 | + public function getMetaAttribute(string | array | null $name, $default = null) |
24 | 31 | { |
25 | 32 | return $this->extra_attributes->get($name, $default); |
26 | 33 | } |
27 | 34 |
|
28 | | - public function setMetaAttribute($name, $value) |
| 35 | + /** |
| 36 | + * Set the meta attribute for the model. |
| 37 | + * |
| 38 | + * @param string|array $name |
| 39 | + * @param mixed $value |
| 40 | + * |
| 41 | + * @return self |
| 42 | + */ |
| 43 | + public function setMetaAttribute(string | array $name, $value): self |
29 | 44 | { |
30 | 45 | $this->extra_attributes->set($name, $value); |
31 | 46 |
|
32 | | - $this->save(); |
| 47 | + return tap($this)->save(); |
33 | 48 | } |
34 | 49 |
|
35 | | - public function hasMetaAttribute($name) |
| 50 | + /** |
| 51 | + * Determine whether the model contains a given meta attribute. |
| 52 | + * |
| 53 | + * @param string $name |
| 54 | + * |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + public function hasMetaAttribute(string $name): bool |
36 | 58 | { |
37 | | - return ! empty($this->extra_attributes->get($name)); |
| 59 | + return $this->extra_attributes->get($name) !== null; |
38 | 60 | } |
39 | 61 |
|
40 | | - public function forgetMetaAttribute($name) |
| 62 | + /** |
| 63 | + * Remove the meta attribute for the model. |
| 64 | + * |
| 65 | + * @param string $name |
| 66 | + * |
| 67 | + * @return self |
| 68 | + */ |
| 69 | + public function forgetMetaAttribute(string $name): self |
41 | 70 | { |
42 | 71 | $this->extra_attributes->forget($name); |
43 | 72 |
|
44 | | - $this->save(); |
| 73 | + return tap($this)->save(); |
45 | 74 | } |
46 | 75 |
|
47 | | - public function fillMetaAttributes($attributes) |
| 76 | + /** |
| 77 | + * Fill the model's meta attributes. |
| 78 | + * |
| 79 | + * @param array $attributes |
| 80 | + * |
| 81 | + * @return self |
| 82 | + */ |
| 83 | + public function fillMetaAttributes(array $attributes): self |
48 | 84 | { |
49 | 85 | foreach ($attributes as $name => $value) { |
50 | 86 | $this->extra_attributes->set($name, $value); |
51 | 87 | } |
52 | 88 |
|
53 | | - $this->save(); |
| 89 | + return tap($this)->save(); |
54 | 90 | } |
55 | 91 |
|
| 92 | + /** |
| 93 | + * Update or create model with the schemaless attributes. |
| 94 | + * |
| 95 | + * @param array $attributes |
| 96 | + * @param array $values |
| 97 | + * |
| 98 | + * @return self |
| 99 | + */ |
56 | 100 | public static function updateOrCreateWithMeta(array $attributes, array $values): Model |
57 | 101 | { |
58 | 102 | $model = static::withExtraAttributes($attributes)->first(); |
|
0 commit comments