Skip to content

Commit 9d2a56b

Browse files
committed
values getter
1 parent b599f06 commit 9d2a56b

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/Concerns/Schemata.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public static function getTabsSchema(): array
197197
return BoltPlugin::getModel('Category')::query()->whereBelongsTo(Filament::getTenant());
198198
},
199199
)
200+
->searchable(false)
200201
->helperText(__('optional, organize your forms into categories'))
201202
->createOptionForm([
202203
TextInput::make('name')
@@ -213,7 +214,7 @@ public static function getTabsSchema(): array
213214
TextInput::make('slug')->required()->maxLength(255)->label(__('slug')),
214215
])
215216
->createOptionAction(fn (Action $action) => $action->hidden(auth()->user()->cannot('create', BoltPlugin::getModel('Category'))))
216-
->getOptionLabelFromRecordUsing(fn (Category $record) => $record->name),
217+
->getOptionLabelFromRecordUsing(fn (?Category $record, $livewire) => $record?->getTranslation('name',$livewire->activeLocale) ?? $record->name),
217218
]),
218219

219220
Tabs\Tab::make('text-details-tab')

src/Models/Collection.php

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaraZeus\Bolt\Models;
44

5+
use Illuminate\Database\Eloquent\Casts\Attribute;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67
use Illuminate\Database\Eloquent\Factories\HasFactory;
78
use Illuminate\Database\Eloquent\Model;
@@ -24,11 +25,11 @@ class Collection extends Model
2425

2526
protected $guarded = [];
2627

27-
public $translatable = ['name', 'values'];
28+
public array $translatable = ['name', 'values'];
2829

2930
public function getTable(): string
3031
{
31-
return config('zeus-bolt.table-prefix') . 'collections';
32+
return config('zeus-bolt.table-prefix').'collections';
3233
}
3334

3435
public function getValuesListAttribute(): ?string
@@ -47,22 +48,38 @@ public function getValuesListAttribute(): ?string
4748
return null;
4849
}
4950

51+
protected function name(): Attribute
52+
{
53+
return Attribute::make(
54+
get: fn($value) => (filled($value))
55+
? $value
56+
: $this->getRawOriginal('name'),
57+
);
58+
}
59+
5060
/**
51-
* Returns the values as a collection. Translatable variables are always cast as an array. This function transforms
52-
* it to a collection.
53-
* Note: The newer Attribute approach does not seem to be compatible with laravel-translatable ;-(.
54-
* @param $value
55-
* @return \Illuminate\Support\Collection
61+
* Returns the values as a collection. Translatable variables are always cast as an array.
62+
* This function transforms it to a collection.
63+
*
64+
* @return Attribute
65+
* @throws \JsonException
5666
*/
57-
public function getValuesAttribute($value)
67+
protected function values(): Attribute
5868
{
59-
if(is_array($value)){
60-
return collect($value);
61-
}
62-
if(is_string($value)){
63-
return collect(json_encode($value));
64-
}
65-
return $value;
69+
return Attribute::make(
70+
get: function ($value) {
71+
$value = (filled($value)) ? $value : $this->getRawOriginal('values');
72+
73+
if (is_string($value)) {
74+
$value = collect(json_decode($value, JSON_THROW_ON_ERROR, 512, JSON_THROW_ON_ERROR));
75+
}
76+
77+
if (is_array($value)) {
78+
$value = collect($value);
79+
}
80+
return $value;
81+
},
82+
);
6683
}
6784

6885
protected static function newFactory(): Factory

0 commit comments

Comments
 (0)