77use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
88use Binaryk \LaravelRestify \MCP \Requests \McpRequest ;
99use Binaryk \LaravelRestify \Repositories \Repository ;
10- use Binaryk \LaravelRestify \Restify ;
1110use Binaryk \LaravelRestify \Traits \HasColumns ;
1211use Binaryk \LaravelRestify \Traits \Make ;
1312use Illuminate \Contracts \Database \Eloquent \Builder ;
@@ -66,6 +65,7 @@ public function resolveField(Repository $repository): EagerField
6665 {
6766 return $ this
6867 ->field
68+ ->forMcp ($ repository ->isForMcp ())
6969 ->columns ($ this ->getColumns ())
7070 ->resolve ($ repository );
7171 }
@@ -107,22 +107,22 @@ public function resolve(RestifyRequest $request, Repository $repository): self
107107 return $ this ;
108108 }
109109
110+
110111 switch ($ paginator ) {
111112 case $ paginator instanceof Collection:
112- $ this ->value = $ this -> serializeRelationshipData ( $ request , $ paginator) ;
113+ $ this ->value = $ paginator ;
113114
114115 break ;
115116 case $ paginator instanceof BelongsTo:
116- $ relatedModel = $ paginator ->first ();
117- $ this ->value = $ relatedModel ? $ this ->serializeRelationshipData ($ request , $ relatedModel ) : null ;
117+ $ this ->value = $ paginator ->first ();
118118
119119 break ;
120120 case $ paginator instanceof Builder:
121- $ this ->value = $ this -> serializeRelationshipData ( $ request , $ paginator ->get () );
121+ $ this ->value = $ paginator ->get ();
122122
123123 break ;
124124 default :
125- $ this ->value = $ this -> serializeRelationshipData ( $ request , $ paginator) ;
125+ $ this ->value = $ paginator ;
126126 }
127127
128128 return $ this ;
@@ -135,74 +135,6 @@ public function resolveUsing(callable $resolver): self
135135 return $ this ;
136136 }
137137
138- /**
139- * Serialize relationship data using repository field collections for MCP requests.
140- */
141- protected function serializeRelationshipData (RestifyRequest $ request , $ data )
142- {
143- // For non-MCP requests, return data as-is to maintain backward compatibility
144- if (! $ request instanceof McpRequest) {
145- return $ data ;
146- }
147-
148- // Handle null data
149- if (is_null ($ data )) {
150- return null ;
151- }
152-
153- // Handle single models
154- if ($ data instanceof \Illuminate \Database \Eloquent \Model) {
155- return $ this ->serializeSingleModel ($ request , $ data );
156- }
157-
158- // Handle collections
159- if ($ data instanceof Collection) {
160- return $ data ->map (function ($ model ) use ($ request ) {
161- return $ model instanceof \Illuminate \Database \Eloquent \Model
162- ? $ this ->serializeSingleModel ($ request , $ model )
163- : $ model ;
164- });
165- }
166-
167- return $ data ;
168- }
169-
170- /**
171- * Serialize a single model using its repository's field collection for MCP requests.
172- */
173- protected function serializeSingleModel (RestifyRequest $ request , \Illuminate \Database \Eloquent \Model $ model ): array
174- {
175- // Try to find the repository for this model
176- $ repositoryClass = Restify::repositoryForModel ($ model );
177-
178- if (! $ repositoryClass ) {
179- // Fallback to model attributes if no repository found
180- return $ model ->toArray ();
181- }
182-
183- try {
184- // Create repository instance with the model
185- $ repository = $ repositoryClass ::resolveWith ($ model );
186- $ repository ->request = $ request ;
187-
188- // Get the appropriate field collection for MCP index
189- $ fields = $ repository ->collectFields ($ request );
190-
191- // Serialize using repository fields
192- $ result = [];
193- foreach ($ fields as $ field ) {
194- $ field ->resolveForIndex ($ repository );
195- $ serialized = $ field ->serializeToValue ($ request );
196- $ result = array_merge ($ result , $ serialized );
197- }
198-
199- return $ result ;
200- } catch (\Exception $ e ) {
201- // Fallback to model attributes if serialization fails
202- return $ model ->toArray ();
203- }
204- }
205-
206138 public function withRelatedQuery (RelatedQuery $ relatedQuery ): self
207139 {
208140 $ this ->relatedQuery = $ relatedQuery ;
0 commit comments