@@ -368,6 +368,7 @@ public function getRelation(RequestStory $request, $relation) {
368
368
'name ' => $ response ['name ' ],
369
369
'published_at ' => $ response ['published_at ' ],
370
370
'full_slug ' => $ response ['full_slug ' ],
371
+ 'page_uuid ' => $ relation ,
371
372
]);
372
373
373
374
return $ relationClass ;
@@ -376,6 +377,51 @@ public function getRelation(RequestStory $request, $relation) {
376
377
}
377
378
}
378
379
380
+ /**
381
+ * Returns an inverse relationship to the current Block. For example if Service has a Multi-Option field
382
+ * relationship to People, on People you can request all the Services it has been related to
383
+ *
384
+ * @param $foreignRelationshipField
385
+ * @param $foreignRelationshipType
386
+ * @param $components
387
+ * @param $options
388
+ * @return array
389
+ */
390
+ public function inverseRelation ($ foreignRelationshipField , $ foreignRelationshipType = 'multi ' , $ components = null , $ options = null ) {
391
+ $ storyblokClient = resolve ('Storyblok\Client ' );
392
+
393
+ $ type = 'any_in_array ' ;
394
+
395
+ if ($ foreignRelationshipType === 'single ' ) {
396
+ $ type = 'in ' ;
397
+ }
398
+
399
+ $ query = [
400
+ 'filter_query ' => [
401
+ $ foreignRelationshipField => [$ type => $ this ->meta ('page_uuid ' ) ?? $ this ->page ()->uuid ()]
402
+ ],
403
+ ];
404
+
405
+ if ($ components ) {
406
+ $ query = array_merge_recursive ($ query , [
407
+ 'filter_query ' => [
408
+ 'component ' => ['in ' => $ components ],
409
+ ]
410
+ ]);
411
+ }
412
+
413
+ if ($ options ) {
414
+ $ query = array_merge_recursive ($ query , $ options );
415
+ }
416
+
417
+ $ storyblokClient ->getStories ($ query );
418
+
419
+ return [
420
+ 'headers ' => $ storyblokClient ->getHeaders (),
421
+ 'stories ' => $ storyblokClient ->getBody ()['stories ' ],
422
+ ];
423
+ }
424
+
379
425
public function getCasts () {
380
426
return $ this ->_casts ;
381
427
}
0 commit comments