# Does current page exist elsewhere in a Structure field?

**URL:** <https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 24, 2021, 6:02pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738 "2021-03-24T18:02:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 24, 2021, 6:02pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/1 "2021-03-24T18:02:08Z")

</div>

I have a list of products that are logged in a page field inside a structure field on the user’s account when purchased. I’m try to do a check on the product page to see if the product exists in the structure field of the user’s content file like this:

```
if($kirby->user()){
    if ($kirby->user()->products()->toStrucure()->findBy('product', $page)) {
        $purchased = true;
    } else {
        $purchased = false;
    }
}

```

This doesn’t work though, `$purchased` is true regardless.

I’m echoing the values like this check if they should be matching or not:

```
<?php
$purchases = $kirby->user()->products()->toStructure();
foreach($purchases as $purchase){
    echo $purchase->product()->toPage();
};
echo $page;
?>

```

It and looks as though it should work as I intend. Where am I going wrong here?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 24, 2021, 7:25pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/3 "2021-03-24T19:25:50Z")

</div>

You cannot use `findBy` here. $page is a page object, but `product` is not.

```auto
$purchased = $kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;
})->isNotEmpty();

```

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 25, 2021, 8:14am UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/4 "2021-03-25T08:14:32Z")

</div>

I thought it might be something like that (using toPage()) but couldn’t quite work it out. Thank you!

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 25, 2021, 10:54am UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/5 "2021-03-25T10:54:43Z")

</div>

What’s the best way to access the found items? `->isNotEmpty()` is returning a bool, if I remove that, `$purchased` is a structure object with integers for where the found items were in the structure I filtered.

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [March 25, 2021, 10:59am UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/6 "2021-03-25T10:59:07Z")

</div>

Since

```auto
$kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;
})

```

return a normal structure object, you can loop through the items as usual.

If you only want the first item, chain `first()` onto it and then access the fields:

```auto
if ($item = $kirby->user()->products()->toStrucure()->filter(function($item) use($page) {
  return $item->product()->toPage() === $page;
})->first()) {
  echo $item->whateverfieldthereis();
}

```

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 25, 2021, 11:08am UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/7 "2021-03-25T11:08:56Z")

</div>

Ah yes, I was completely overcomplicating this. Thank you!

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 31, 2021, 1:45pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/8 "2021-03-31T13:45:36Z")

</div>

I have a setup where a product ($page) can exist elsewhere in a pages field as part of a bundle. I’m trying to check if the user owns the bundle that contains the product, but as the user can own multiple bundles that contain the product I’d like to get them all, sort by date, and select the most recent:

```
//Get All Bundles
$allBundles = page('products')->children()->template('bundle');
//Check which Bundles Product exists in
$inBundles = $allBundles->filter(function($bundle) use($page) {
    return $bundle->products()->toPages()->has($page);
});
//Check if user owns Bundle <----- The bit I can't work out
foreach($inBundles as $bundle){
    $purchased = $kirby->user()->products()->toStructure()->filter(function($item) use($bundle) {
        return $item->product()->toPage() === $bundle;
    });
}

```

`$purchased` here just keeps getting overwritten. How can I add items to `$purchased` for filtering later?

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [March 31, 2021, 2:14pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/9 "2021-03-31T14:14:44Z")

</div>

Not sure what the expected result is? A set of pages? A set of structure items?

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 31, 2021, 2:20pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/10 "2021-03-31T14:20:28Z")

</div>

As Structure with items. At the moment $purchased is a structure with a single item that is being returned multiple times because of the `foreach` (when `$inBundles` has multiple items)

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [March 31, 2021, 2:22pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/11 "2021-03-31T14:22:34Z")

</div>

Then you would have to collect the items into a new Structure object.

```auto
$purchased = new Structure();
// then add elements to structure in loop

```

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [March 31, 2021, 2:37pm UTC](https://forum.getkirby.com/t/does-current-page-exist-elsewhere-in-a-structure-field/21738/12 "2021-03-31T14:37:41Z")

</div>

Of course, thanks!
