44
55namespace Cone \Root \Fields ;
66
7- use Cone \Root \Http \Controllers \MorphToController ;
87use Illuminate \Database \Eloquent \Builder ;
98use Illuminate \Database \Eloquent \Model ;
109use Illuminate \Database \Eloquent \Relations \MorphTo as EloquentRelation ;
1110use Illuminate \Http \Request ;
12- use Illuminate \Routing \Router ;
1311use Illuminate \Support \Str ;
1412
1513/**
@@ -20,7 +18,7 @@ class MorphTo extends BelongsTo
2018 /**
2119 * The Blade template.
2220 */
23- protected string $ template = 'root::fields.morph-to ' ;
21+ protected string $ template = 'root::fields.relation ' ;
2422
2523 /**
2624 * The morph types.
@@ -35,6 +33,29 @@ public function getRelation(Model $model): EloquentRelation
3533 return parent ::getRelation ($ model );
3634 }
3735
36+ /**
37+ * {@inheritdoc}
38+ */
39+ public function filters (Request $ request ): array
40+ {
41+ $ filters = parent ::filters ($ request );
42+
43+ if ($ this ->isAsync ()) {
44+ $ typeFilter = Select::make (__ ('Type ' ), 'type ' )
45+ ->options (array_map (
46+ static function (string $ type ): string {
47+ return __ (Str::of ($ type )->classBasename ()->headline ()->value ());
48+ },
49+ array_combine ($ this ->types , $ this ->types )
50+ ))
51+ ->toFilter ();
52+
53+ array_unshift ($ filters , $ typeFilter );
54+ }
55+
56+ return $ filters ;
57+ }
58+
3859 /**
3960 * {@inheritdoc}
4061 */
@@ -59,11 +80,12 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
5980 {
6081 $ value = is_null ($ value ) ? $ value : explode (': ' , $ value );
6182
62- $ related = is_null ($ value )
63- ? $ value
64- : tap (new $ value [0 ], static function (Model $ related ) use ($ value ): void {
83+ $ related = match (true ) {
84+ ! is_null ($ value ) => tap (new $ value [0 ], static function (Model $ related ) use ($ value ): void {
6585 $ related ->forceFill ([$ related ->getKeyName () => $ value [1 ]]);
66- });
86+ }),
87+ default => $ value ,
88+ };
6789
6890 parent ::resolveHydrate ($ request , $ model , $ related );
6991 }
@@ -86,33 +108,13 @@ public function types(array $types): static
86108 return $ this ;
87109 }
88110
89- /**
90- * {@inheritdoc}
91- */
92- public function routes (Router $ router ): void
93- {
94- $ router ->get ('/ ' , MorphToController::class);
95- }
96-
97- /**
98- * {@inheritdoc}
99- */
100- public function toArray (): array
101- {
102- return array_merge (parent ::toArray (), [
103- 'types ' => array_map (
104- static fn (string $ type ): string => __ (Str::of ($ type )->classBasename ()->headline ()->value ()), array_combine ($ this ->types , $ this ->types )
105- ),
106- ]);
107- }
108-
109111 /**
110112 * {@inheritdoc}
111113 */
112114 public function toInput (Request $ request , Model $ model ): array
113115 {
114116 return array_merge (parent ::toInput ($ request , $ model ), [
115- 'url ' => $ this ->replaceRoutePlaceholders ($ request ->route ()),
117+ 'url ' => sprintf ( ' %s/search ' , $ this ->replaceRoutePlaceholders ($ request ->route () )),
116118 'morphTypeName ' => $ name = $ this ->getRelation ($ model )->getMorphType (),
117119 'morphType ' => $ model ->getAttribute ($ name ),
118120 ]);
0 commit comments