# Using the key from a grouping inside the find method doesn't work

**URL:** <https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 17, 2017, 10:31am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049 "2017-11-17T10:31:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [November 17, 2017, 10:31am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/1 "2017-11-17T10:31:26Z")

</div>

I’m facing a problem with my kirby project right now. In general, the below code works as expected, except for line 4.

```php
<?php
$categories = $page->children()->visible()->groupBy('typ');
foreach($categories as $category => $products): ?>
  <h2><?= page('angebote/kategorien')->children()->find((string)$category)->title() ?></h2>
  <ul class="products grid gutter-1">
    <?php
      foreach ($products as $product): 
        snippet('single-angebot', array('product' => $product)); 
      endforeach;
    ?>
  </ul>
<?php endforeach ?>

```

The problem is, that I want to specify several user-defined categories as pages, in my case they are located in the relative folder `angebote/kategorien`. This folder contains all of the categories with a textfile that only contains a ´Title´ field.

The children of the current page are grouped by a property called `typ` which is basically the uid for the category. Because each category has another display title, I want to fetch the title from this category.

[According to the docs](https://getkirby.com/docs/cheatsheet/pages/find) the `->find(key)` method returns the related page object. And this is true, whether with or without the `(string)` cast. I’ve actually checked that via `dump()`.

**The actual issue now** , is that I cannot call the ´-\>title()´ method. Below is the error mesage.

```
Call to a member function title() on boolean

```

Ironically, it works perfectly when I am hardcoding a specific category like the following example:

```
<h2><?= page('angebote/kategorien')->children()->find('sample')->title() ?></h2>

```

But a `dump($category)` only returns the plain string, nothing special.

Am I doing something wrong or is this a bug? Besides that, is there a more elegant solution for a simple user-manipulateable categories field which is also queryable via the select field on the panel?

---

<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:** [November 17, 2017, 10:38am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/2 "2017-11-17T10:38:37Z")

</div>

> [@jlnostr](#):
>
> \<h2\>\<?= page(‘angebote/kategorien’)-\>children()-\>find((string)$category)-\>title() ?\>\</h2\>

Could you please check if this works and what result you get:

```auto
<h2><?= $catPage = (page('angebote/kategorien')->children()->find($category))? $catPage->title(): 'page does not exist'; ?></h2>

```

---

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [November 17, 2017, 10:53am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/3 "2017-11-17T10:53:27Z")

</div>

The returned error is:

```
Undefined variable: catPage

```

This implies that the page has been found. And when I hardcode a category that doesn’t exist, it also returns “page does not exist” as expected.

---

<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:** [November 17, 2017, 10:55am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/4 "2017-11-17T10:55:08Z")

</div>

> [@texnixe](#):
>
> \<h2\>\<?= $catPage = (page(‘angebote/kategorien’)-\>children()-\>find($category))? $catPage-\>title(): ‘page does not exist’; ?\>\</h2\>

Oh, sorry, wrong placement of parenthesis, try this:

```auto
<h2><?= ($catPage = page('angebote/kategorien')->children()->find($category))? $catPage->title(): 'page does not exist'; ?></h2>

```

---

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [November 17, 2017, 10:57am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/5 "2017-11-17T10:57:39Z")

</div>

Yay, everything works as expected and each category is returning the correct title 🙂  
Thank you!

---

<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:** [November 17, 2017, 11:51am UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/6 "2017-11-17T11:51:46Z")

</div>

I know I’m repeating myself over and over again, but I can only recommend to make this a best practice: Whenever you try to call a method on an object, always check if the object exists first.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:30pm UTC](https://forum.getkirby.com/t/using-the-key-from-a-grouping-inside-the-find-method-doesnt-work/9049/7 "2025-02-06T17:30:29Z")

</div>


