1010use Binaryk \LaravelRestify \Filter ;
1111use Binaryk \LaravelRestify \Http \Requests \RepositoryStoreBulkRequest ;
1212use Binaryk \LaravelRestify \Http \Requests \RestifyRequest ;
13+ use Binaryk \LaravelRestify \Models \CreationAware ;
1314use Binaryk \LaravelRestify \Restify ;
1415use Binaryk \LaravelRestify \Services \Search \RepositorySearchService ;
1516use Binaryk \LaravelRestify \Traits \InteractWithSearch ;
@@ -213,7 +214,7 @@ public static function newModel(): Model
213214
214215 public static function query (RestifyRequest $ request )
215216 {
216- if (! $ request ->isViaRepository ()) {
217+ if (!$ request ->isViaRepository ()) {
217218 return static ::newModel ()->query ();
218219 }
219220
@@ -278,8 +279,8 @@ public function collectFields(RestifyRequest $request)
278279
279280 if ($ this instanceof Mergeable) {
280281 $ fillable = collect ($ this ->resource ->getFillable ())
281- ->filter (fn ($ attribute ) => $ fields ->contains ('attribute ' , $ attribute ) === false )
282- ->map (fn ($ attribute ) => Field::new ($ attribute ));
282+ ->filter (fn ($ attribute ) => $ fields ->contains ('attribute ' , $ attribute ) === false )
283+ ->map (fn ($ attribute ) => Field::new ($ attribute ));
283284
284285 $ fields = $ fields ->merge ($ fillable );
285286 }
@@ -290,14 +291,14 @@ public function collectFields(RestifyRequest $request)
290291 private function indexFields (RestifyRequest $ request ): Collection
291292 {
292293 return $ this ->collectFields ($ request )
293- ->filter (fn (Field $ field ) => ! $ field ->isHiddenOnIndex ($ request , $ this ))
294+ ->filter (fn (Field $ field ) => !$ field ->isHiddenOnIndex ($ request , $ this ))
294295 ->values ();
295296 }
296297
297298 private function showFields (RestifyRequest $ request ): Collection
298299 {
299300 return $ this ->collectFields ($ request )
300- ->filter (fn (Field $ field ) => ! $ field ->isHiddenOnShow ($ request , $ this ))
301+ ->filter (fn (Field $ field ) => !$ field ->isHiddenOnShow ($ request , $ this ))
301302 ->values ();
302303 }
303304
@@ -411,10 +412,10 @@ public static function routes(Router $router, $attributes, $wrap = false)
411412 public function resolveShowAttributes (RestifyRequest $ request )
412413 {
413414 $ fields = $ this ->showFields ($ request )
414- ->filter (fn (Field $ field ) => $ field ->authorize ($ request ))
415- ->each (fn (Field $ field ) => $ field ->resolveForShow ($ this ))
416- ->map (fn (Field $ field ) => $ field ->serializeToValue ($ request ))
417- ->mapWithKeys (fn ($ value ) => $ value )
415+ ->filter (fn (Field $ field ) => $ field ->authorize ($ request ))
416+ ->each (fn (Field $ field ) => $ field ->resolveForShow ($ this ))
417+ ->map (fn (Field $ field ) => $ field ->serializeToValue ($ request ))
418+ ->mapWithKeys (fn ($ value ) => $ value )
418419 ->all ();
419420
420421 if ($ this instanceof Mergeable) {
@@ -432,7 +433,7 @@ public function resolveShowAttributes(RestifyRequest $request)
432433 return false ;
433434 }
434435
435- if (! $ field ->authorize ($ request )) {
436+ if (!$ field ->authorize ($ request )) {
436437 return false ;
437438 }
438439
@@ -453,10 +454,10 @@ public function resolveIndexAttributes($request)
453454 {
454455 // Resolve the show method, and attach the value to the array
455456 $ fields = $ this ->indexFields ($ request )
456- ->filter (fn (Field $ field ) => $ field ->authorize ($ request ))
457- ->each (fn (Field $ field ) => $ field ->resolveForIndex ($ this ))
458- ->map (fn (Field $ field ) => $ field ->serializeToValue ($ request ))
459- ->mapWithKeys (fn ($ value ) => $ value )
457+ ->filter (fn (Field $ field ) => $ field ->authorize ($ request ))
458+ ->each (fn (Field $ field ) => $ field ->resolveForIndex ($ this ))
459+ ->map (fn (Field $ field ) => $ field ->serializeToValue ($ request ))
460+ ->mapWithKeys (fn ($ value ) => $ value )
460461 ->all ();
461462
462463 if ($ this instanceof Mergeable) {
@@ -474,7 +475,7 @@ public function resolveIndexAttributes($request)
474475 return false ;
475476 }
476477
477- if (! $ field ->authorize ($ request )) {
478+ if (!$ field ->authorize ($ request )) {
478479 return false ;
479480 }
480481
@@ -541,7 +542,7 @@ public function resolveRelationships($request): array
541542 }
542543
543544 $ withs [$ relation ] = $ paginator instanceof Collection
544- ? $ paginator ->map (fn (Model $ item ) => [
545+ ? $ paginator ->map (fn (Model $ item ) => [
545546 'attributes ' => $ item ->toArray (),
546547 ])
547548 : $ paginator ;
@@ -598,10 +599,10 @@ public function index(RestifyRequest $request)
598599
599600 return $ this ->response ([
600601 'meta ' => $ this ->resolveIndexMainMeta (
601- $ request , $ items ->map (fn (self $ repository ) => $ repository ->resource ), RepositoryCollection::meta ($ paginator ->toArray ())
602+ $ request , $ items ->map (fn (self $ repository ) => $ repository ->resource ), RepositoryCollection::meta ($ paginator ->toArray ())
602603 ) ?? RepositoryCollection::meta ($ paginator ->toArray ()),
603604 'links ' => RepositoryCollection::paginationLinks ($ paginator ->toArray ()),
604- 'data ' => $ items ->map (fn (self $ repository ) => $ repository ->serializeForIndex ($ request )),
605+ 'data ' => $ items ->map (fn (self $ repository ) => $ repository ->serializeForIndex ($ request )),
605606 ]);
606607 }
607608
@@ -630,10 +631,16 @@ public function store(RestifyRequest $request)
630631 $ this ->resource = $ request ->viaQuery ()
631632 ->save ($ this ->resource );
632633 } else {
633- $ this ->resource ->save ();
634+ if ($ this ->resource instanceof CreationAware) {
635+ $ this ->resource = $ this ->resource ->createWithAttributes (
636+ $ this ->resource ->toArray ()
637+ );
638+ } else {
639+ $ this ->resource ->save ();
640+ }
634641 }
635642
636- $ this ->storeFields ($ request )->each (fn (Field $ field ) => $ field ->invokeAfter ($ request , $ this ->resource ));
643+ $ this ->storeFields ($ request )->each (fn (Field $ field ) => $ field ->invokeAfter ($ request , $ this ->resource ));
637644 });
638645
639646 static ::stored ($ this ->resource , $ request );
@@ -657,7 +664,7 @@ public function storeBulk(RepositoryStoreBulkRequest $request)
657664
658665 $ this ->resource ->save ();
659666
660- $ this ->storeBulkFields ($ request )->each (fn (Field $ field ) => $ field ->invokeAfter ($ request , $ this ->resource ));
667+ $ this ->storeBulkFields ($ request )->each (fn (Field $ field ) => $ field ->invokeAfter ($ request , $ this ->resource ));
661668
662669 return $ this ->resource ;
663670 });
@@ -704,8 +711,8 @@ public function updateBulk(RestifyRequest $request, $repositoryId, int $row)
704711 public function attach (RestifyRequest $ request , $ repositoryId , Collection $ pivots )
705712 {
706713 DB ::transaction (function () use ($ request , $ pivots ) {
707- return $ pivots ->map (fn ($ pivot ) => $ pivot ->forceFill ($ request ->except ($ request ->relatedRepository )))
708- ->map (fn ($ pivot ) => $ pivot ->save ());
714+ return $ pivots ->map (fn ($ pivot ) => $ pivot ->forceFill ($ request ->except ($ request ->relatedRepository )))
715+ ->map (fn ($ pivot ) => $ pivot ->save ());
709716 });
710717
711718 return $ this ->response ()
@@ -716,7 +723,7 @@ public function attach(RestifyRequest $request, $repositoryId, Collection $pivot
716723 public function detach (RestifyRequest $ request , $ repositoryId , Collection $ pivots )
717724 {
718725 $ deleted = DB ::transaction (function () use ($ pivots ) {
719- return $ pivots ->map (fn ($ pivot ) => $ pivot ->delete ());
726+ return $ pivots ->map (fn ($ pivot ) => $ pivot ->delete ());
720727 });
721728
722729 return $ this ->response ()
@@ -826,7 +833,7 @@ public function response($content = '', $status = 200, array $headers = []): Res
826833 public function serializeForShow (RestifyRequest $ request ): array
827834 {
828835 return $ this ->filter ([
829- 'id ' => $ this ->when ($ this ->resource ->id , fn () => $ this ->getShowId ($ request )),
836+ 'id ' => $ this ->when ($ this ->resource ->id , fn () => $ this ->getShowId ($ request )),
830837 'type ' => $ this ->when ($ type = $ this ->getType ($ request ), $ type ),
831838 'attributes ' => $ request ->isShowRequest () ? $ this ->resolveShowAttributes ($ request ) : $ this ->resolveIndexAttributes ($ request ),
832839 'relationships ' => $ this ->when (value ($ related = $ this ->resolveRelationships ($ request )), $ related ),
@@ -839,7 +846,7 @@ public function serializeForIndex(RestifyRequest $request): array
839846 return $ this ->filter ([
840847 'id ' => $ this ->when ($ id = $ this ->getShowId ($ request ), $ id ),
841848 'type ' => $ this ->when ($ type = $ this ->getType ($ request ), $ type ),
842- 'attributes ' => $ this ->when ((bool ) $ attrs = $ this ->resolveIndexAttributes ($ request ), $ attrs ),
849+ 'attributes ' => $ this ->when ((bool )$ attrs = $ this ->resolveIndexAttributes ($ request ), $ attrs ),
843850 'relationships ' => $ this ->when (value ($ related = $ this ->resolveRelationships ($ request )), $ related ),
844851 'meta ' => $ this ->when (value ($ meta = $ this ->resolveIndexMeta ($ request )), $ meta ),
845852 ]);
@@ -889,12 +896,12 @@ protected static function fillBulkFields(RestifyRequest $request, Model $model,
889896
890897 public static function uriTo (Model $ model )
891898 {
892- return Restify::path (). '/ ' . static ::uriKey (). '/ ' . $ model ->getKey ();
899+ return Restify::path () . '/ ' . static ::uriKey () . '/ ' . $ model ->getKey ();
893900 }
894901
895902 public function availableFilters (RestifyRequest $ request )
896903 {
897- return collect ($ this ->filter ($ this ->filters ($ request )))->each (fn (Filter $ filter ) => $ filter ->authorizedToSee ($ request ))
904+ return collect ($ this ->filter ($ this ->filters ($ request )))->each (fn (Filter $ filter ) => $ filter ->authorizedToSee ($ request ))
898905 ->values ();
899906 }
900907
0 commit comments