@@ -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}
0 commit comments