Use options from "Relationship field" plugin in loop?

Hello everybody,

I’m using the plugin “Relationship field” to select subpages to be shown on an overview-page (some text and image of every subpage).

With the blueprint I managed to fetch the subpages over the templates (I could also fetch them like $site->index()->filterBy('depth', 3)->visible() because they are all in the same depth if something like this is possible/easier?):

fields:
  projekte:
    label: Show projects on page
    type: relationship
    options: query
    query:
      fetch: pages
      template: ['template1', 'template2', 'template3', 'template4']

How can I now use those selected subpages for a loop? It should do something like this:

<?php foreach($projekte as $projekt): ?>

  <?php if($image = $projekt->coverImage()->toFile()):
    $thumb = $image->thumb(array('width' => 900, 'height' => 600, 'crop' => true)); ?>
  <?php endif ?>

  <?= $project->title()->html() ?>
  <img src="<?= $thumb->url() ?>" />

<?php endforeach ?>

Thanks a lot already for inputs !

With your current setup in your blueprint, only the UID of the page is saved. You would have to change this, so that the URI is saved instead:

fields:
  projekte:
    label: Show projects on page
    type: relationship
    options: query
    query:
      fetch: pages
      value: '{{uri}}'
      template: ['template1', 'template2', 'template3', 'template4']

Secondly, in your template, you can then fetch the pages like this:

<?php foreach($page->projekte()->pages(',') as $project): ?>

  <?php if($image = $project->coverImage()->toFile()):
    $thumb = $image->thumb(array('width' => 900, 'height' => 600, 'crop' => true)); ?>
  <?php endif ?>

  <?= $project->title()->html() ?>
  <img src="<?= $thumb->url() ?>" />

<?php endforeach ?>

(make sure not to mix $projekt and $project)

Thanks a lot @texnixe ! This is perfectly working so far. Is there also a way to achive something like this? I need to divide the pages in different groups before they are used in the loops :

<?php $first = $page->projekte()->visible()->first(); ?>
<?php $group1 = $page->projekte()->visible(); ?>
<?php $group2 = $page->projekte()->visible()->not($first); ?>

<?php foreach($group1 as $1): ?>
  // Do something
<?php endforeach ?>

<?php foreach($group2 as $2): ?>
  // Do something
<?php endforeach ?>

Thanks a lot already !

Sure, you get your page collection like this:

$collection = $page->projekte()->pages(',');

Then. you can work it from there

$first = $collection->visible()->first(); ?>
$group1 = $collection->visible(); 
$group2 = $collection->visible()->not($first);

Thank you very much @texnixe, this works perfectly !