Skip to content

Commit d184494

Browse files
authored
refactor: update schemaless attributes for v2 (#18)
1 parent e1e2627 commit d184494

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

examples/ui.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
# Simple Examples
1+
# Development
2+
3+
## Schemaless Attributes
4+
5+
In order to use the `HasSchemalessAttributes` trait, you will need to ensure the model has the correct casting applied:
6+
7+
```php
8+
<?php
9+
10+
use ...;
11+
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
12+
13+
class User extends Model
14+
{
15+
protected $casts = [
16+
'extra_attributes' => SchemalessAttributes::class,
17+
];
18+
}
19+
```
20+
21+
# Component Examples
222

323
This file contains basic examples and explains the parameters that can be used for the components.
424

src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,97 @@
66

77
use Illuminate\Database\Eloquent\Builder;
88
use Illuminate\Database\Eloquent\Model;
9-
use Spatie\SchemalessAttributes\SchemalessAttributes;
109

1110
trait HasSchemalessAttributes
1211
{
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+
*/
1817
public function scopeWithExtraAttributes(): Builder
1918
{
20-
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
19+
return $this->extra_attributes->modelScope();
2120
}
2221

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)
2431
{
2532
return $this->extra_attributes->get($name, $default);
2633
}
2734

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
2944
{
3045
$this->extra_attributes->set($name, $value);
3146

32-
$this->save();
47+
return tap($this)->save();
3348
}
3449

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
3658
{
37-
return ! empty($this->extra_attributes->get($name));
59+
return $this->extra_attributes->get($name) !== null;
3860
}
3961

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
4170
{
4271
$this->extra_attributes->forget($name);
4372

44-
$this->save();
73+
return tap($this)->save();
4574
}
4675

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
4884
{
4985
foreach ($attributes as $name => $value) {
5086
$this->extra_attributes->set($name, $value);
5187
}
5288

53-
$this->save();
89+
return tap($this)->save();
5490
}
5591

92+
/**
93+
* Update or create model with the schemaless attributes.
94+
*
95+
* @param array $attributes
96+
* @param array $values
97+
*
98+
* @return self
99+
*/
56100
public static function updateOrCreateWithMeta(array $attributes, array $values): Model
57101
{
58102
$model = static::withExtraAttributes($attributes)->first();

0 commit comments

Comments
 (0)