Skip to content

Commit 464ccb4

Browse files
committed
Allow user to define the valuesColumn
1 parent 970a0de commit 464ccb4

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ php artisan migrate
9999

100100
When one of your models has configurable fields, you need to add the `HasFields` trait to your model.
101101

102+
The trait will add a `fields` relation to your model, and define the `valueColumn` property. This is the column that will be used to store the field values. Because the values are stored as json, you should cast this column to an array.
103+
104+
If you want to use any other column name for the values, you can set the `valueColumn` property in your model.
105+
102106
```php
103107
use Illuminate\Database\Eloquent\Model;
104-
use Illuminate\Database\Eloquent\Relations\MorphMany;
105-
use Vormkracht10\Fields\Models\Field;
108+
use Vormkracht10\Fields\Concerns\HasFields;
106109

107110
class Content extends Model
108111
{

src/Concerns/CanMapDynamicFields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function mutateFormDataBeforeFill(array $data): array
6464
return $fieldInstance->mutateFormDataCallback($this->record, $field, $data);
6565
}
6666

67-
$data['values'][$field->slug] = $this->record->values[$field->slug] ?? null;
67+
$data[$this->record->valueColumn][$field->slug] = $this->record->values[$field->slug] ?? null;
6868

6969
return $data;
7070
});
@@ -124,7 +124,7 @@ private function resolveCustomFields(): Collection
124124

125125
private function resolveFieldInput(Model $field, Collection $customFields): ?object
126126
{
127-
$inputName = "values.{$field->slug}";
127+
$inputName = "{$this->record->valueColumn}.{$field->slug}";
128128

129129
// Try to resolve from standard field type map
130130
if ($fieldClass = self::FIELD_TYPE_MAP[$field->field_type] ?? null) {

src/Concerns/HasFields.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77

88
trait HasFields
99
{
10+
public string $valueColumn = 'values';
11+
12+
protected function casts(): array
13+
{
14+
return [
15+
$this->valueColumn => 'array',
16+
];
17+
}
18+
1019
public function fields(): MorphMany
1120
{
1221
return $this->morphMany(Field::class, 'slug', 'model_type', 'model_key')
1322
->orderBy('position');
1423
}
15-
}
24+
}

0 commit comments

Comments
 (0)