2424use Binaryk \LaravelRestify \Traits \InteractWithSearch ;
2525use Binaryk \LaravelRestify \Traits \PerformsQueries ;
2626use Illuminate \Contracts \Pagination \LengthAwarePaginator ;
27- use Illuminate \Database \Eloquent \Builder ;
2827use Illuminate \Database \Eloquent \Model ;
29- use Illuminate \Database \Eloquent \Relations \Relation ;
3028use Illuminate \Http \Request ;
3129use Illuminate \Http \Resources \ConditionallyLoadsAttributes ;
3230use Illuminate \Http \Resources \DelegatesToResource ;
3331use Illuminate \Pagination \AbstractPaginator ;
3432use Illuminate \Routing \Router ;
35- use Illuminate \Support \Arr ;
3633use Illuminate \Support \Collection ;
3734use Illuminate \Support \Facades \DB ;
3835use Illuminate \Support \Str ;
@@ -575,9 +572,7 @@ public function resolveIndexPivots(RestifyRequest $request): array
575572 */
576573 public function resolveRelationships ($ request ): array
577574 {
578- $ withs = collect ();
579-
580- static ::collectRelated ()
575+ return static ::collectRelated ()
581576 ->authorized ($ request )
582577 ->inRequest ($ request )
583578 ->when ($ request ->isShowRequest (), function (RelatedCollection $ collection ) use ($ request ) {
@@ -587,39 +582,9 @@ public function resolveRelationships($request): array
587582 return $ collection ->forIndex ($ request , $ this );
588583 })
589584 ->mapIntoRelated ($ request )
590- ->each (function (Related $ related ) use ($ request , $ withs ) {
591- $ relation = $ related ->getRelation ();
592-
593- if (Str::contains ($ relation , '. ' )) {
594- $ this ->resource ->loadMissing ($ relation );
595-
596- return $ withs ->put ($ key = Str::before ($ relation , '. ' ), Arr::get ($ this ->resource ->relationsToArray (), $ key ));
597- }
598-
599- /** * To avoid circular relationships and deep stack calls, we will do not load eager fields. */
600- if ($ related ->isEager () && $ this ->isEagerState () === false ) {
601- return $ withs ->put ($ relation , $ related ->resolveField ($ this )->value );
602- }
603-
604- $ paginator = $ this ->resource ->relationLoaded ($ relation )
605- ? $ this ->resource ->{$ relation }
606- : $ this ->resource ->{$ relation }();
607-
608- collect ([
609- Builder::class => fn () => $ withs ->put ($ relation , (static ::$ relatedCast )::fromBuilder ($ request , $ paginator , $ this )),
610-
611- Relation::class => fn () => $ withs ->put ($ relation , (static ::$ relatedCast )::fromRelation ($ request , $ paginator , $ this )),
612-
613- Collection::class => fn () => $ withs ->put ($ relation , $ paginator ),
614-
615- Model::class => fn () => fn () => $ withs ->put ($ relation , $ paginator ),
616-
617- ])->first (fn ($ fn , $ class ) => $ paginator instanceof $ class ,
618- fn () => fn () => $ withs ->put ($ relation , $ paginator )
619- )();
620- });
621-
622- return $ withs ->all ();
585+ ->map (function (Related $ related ) use ($ request ) {
586+ return $ related ->resolve ($ request , $ this )->getValue ();
587+ })->all ();
623588 }
624589
625590 /**
@@ -830,6 +795,8 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot
830795 $ fields = $ eagerField ->collectPivotFields ()->filter (fn ($ pivotField ) => $ request ->has ($ pivotField ->attribute ))->values ();
831796
832797 $ pivots ->map (function ($ pivot ) use ($ request , $ fields , $ eagerField ) {
798+ $ eagerField ->validate ($ request , $ pivot );
799+
833800 static ::validatorForAttach ($ request )->validate ();
834801
835802 static ::fillFields ($ request , $ pivot , $ fields );
0 commit comments