Skip to content

Commit ba61ff4

Browse files
committed
wip
1 parent 037c057 commit ba61ff4

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

src/Filament/RelationManagers/FieldsRelationManager.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,10 @@ public function form(Schema $schema): Schema
124124
Select::make('schema_id')
125125
->label(__('Attach to Schema'))
126126
->placeholder(__('Select a schema (optional)'))
127-
->relationship(
128-
name: 'schema',
129-
titleAttribute: 'name',
130-
modifyQueryUsing: function ($query) {
131-
$key = $this->ownerRecord->getKeyName();
132-
133-
return $query->where('schemas.model_key', $this->ownerRecord->{$key})
134-
->where('schemas.model_type', get_class($this->ownerRecord))
135-
->orderBy('schemas.position');
136-
}
137-
)
127+
->options($this->getSchemaOptions())
138128
->searchable()
129+
->live()
130+
->reactive()
139131
->helperText(__('Attach this field to a specific schema for better organization')),
140132

141133
]),
@@ -157,7 +149,7 @@ public function table(Table $table): Table
157149
->recordTitleAttribute('name')
158150
->reorderable('position')
159151
->defaultSort('position', 'asc')
160-
->defaultGroup('schema.slug')
152+
->modifyQueryUsing(fn ($query) => $query->with(['schema']))
161153
->columns([
162154
TextColumn::make('name')
163155
->label(__('Name'))
@@ -179,7 +171,6 @@ public function table(Table $table): Table
179171
->label(__('Schema'))
180172
->placeholder(__('No schema'))
181173
->searchable()
182-
->sortable()
183174
->getStateUsing(fn (Field $record): string => $record->schema->name ?? __('No Schema')),
184175
])
185176
->filters([
@@ -205,11 +196,12 @@ public function table(Table $table): Table
205196
->slideOver()
206197
->mutateDataUsing(function (array $data) {
207198

208-
$key = $this->ownerRecord->getKeyName();
209-
210199
return [
211200
...$data,
212-
'position' => Field::where('model_key', $key)->get()->max('position') + 1,
201+
'position' => Field::where('model_key', $this->ownerRecord->getKey())
202+
->where('model_type', get_class($this->ownerRecord))
203+
->get()
204+
->max('position') + 1,
213205
'model_type' => get_class($this->ownerRecord),
214206
'model_key' => $this->ownerRecord->getKey(),
215207
];
@@ -223,12 +215,10 @@ public function table(Table $table): Table
223215
->slideOver()
224216
->mutateRecordDataUsing(function (array $data) {
225217

226-
$key = $this->ownerRecord->getKeyName();
227-
228218
return [
229219
...$data,
230220
'model_type' => get_class($this->ownerRecord),
231-
'model_key' => $this->ownerRecord->{$key},
221+
'model_key' => $this->ownerRecord->getKey(),
232222
];
233223
})
234224
->after(function (Component $livewire) {
@@ -277,4 +267,27 @@ public static function getPluralModelLabel(): string
277267
{
278268
return __('Fields');
279269
}
270+
271+
protected function getSchemaOptions(): array
272+
{
273+
if (! $this->ownerRecord) {
274+
return [];
275+
}
276+
277+
$options = \Backstage\Fields\Models\Schema::where('model_key', $this->ownerRecord->getKey())
278+
->where('model_type', get_class($this->ownerRecord))
279+
->orderBy('position')
280+
->pluck('name', 'ulid')
281+
->toArray();
282+
283+
// Debug: Log the options to help troubleshoot
284+
\Log::info('Schema options for owner record', [
285+
'owner_record_id' => $this->ownerRecord->getKey(),
286+
'owner_record_class' => get_class($this->ownerRecord),
287+
'options_count' => count($options),
288+
'options' => $options,
289+
]);
290+
291+
return $options;
292+
}
280293
}

src/Filament/RelationManagers/SchemaRelationManager.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function table(Table $table): Table
121121
->recordTitleAttribute('name')
122122
->reorderable('position')
123123
->defaultSort('position', 'asc')
124-
->defaultGroup('parent.name')
124+
->modifyQueryUsing(fn ($query) => $query->with(['parent']))
125125
->columns([
126126
TextColumn::make('name')
127127
->label(__('Name'))
@@ -135,8 +135,7 @@ public function table(Table $table): Table
135135
TextColumn::make('parent.name')
136136
->label(__('Parent Schema'))
137137
->placeholder(__('Root level'))
138-
->searchable()
139-
->sortable(),
138+
->searchable(),
140139
])
141140
->filters([])
142141
->headerActions([
@@ -148,7 +147,7 @@ public function table(Table $table): Table
148147
$parentUlid = $data['parent_ulid'] ?? null;
149148

150149
// Calculate position based on parent
151-
$positionQuery = SchemaModel::where('model_key', $key)
150+
$positionQuery = SchemaModel::where('model_key', $this->ownerRecord->{$key})
152151
->where('model_type', get_class($this->ownerRecord));
153152

154153
if ($parentUlid) {
@@ -173,12 +172,10 @@ public function table(Table $table): Table
173172
->slideOver()
174173
->mutateRecordDataUsing(function (array $data) {
175174

176-
$key = $this->ownerRecord->getKeyName();
177-
178175
return [
179176
...$data,
180-
'model_type' => 'setting',
181-
'model_key' => $this->ownerRecord->{$key},
177+
'model_type' => get_class($this->ownerRecord),
178+
'model_key' => $this->ownerRecord->getKey(),
182179
];
183180
})
184181
->after(function (Component $livewire) {

src/Models/Schema.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ public function children(): HasMany
7070
{
7171
return $this->hasMany(Schema::class, 'parent_ulid');
7272
}
73+
74+
public function getParentNameAttribute(): ?string
75+
{
76+
return $this->parent?->name;
77+
}
7378
}

0 commit comments

Comments
 (0)