Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion examples/ui.md
Original file line number Diff line number Diff line change
@@ -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
<?php

use ...;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;

class User extends Model
{
protected $casts = [
'extra_attributes' => SchemalessAttributes::class,
];
}
```

# Component Examples

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

Expand Down
76 changes: 60 additions & 16 deletions src/UserInterface/Eloquent/Concerns/HasSchemalessAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,97 @@

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');
}

/**
* Scope the query to include the meta attributes.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWithExtraAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('extra_attributes');
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();
Expand Down