# \`Call to a member function url() on boolean\`on selectfield for URL

**URL:** <https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225>\
**Category:** Questions\
**Tags:** v2\
**Created:** [April 14, 2018, 7:17pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225 "2018-04-14T19:17:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [April 14, 2018, 7:17pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/1 "2018-04-14T19:17:38Z")

</div>

Hi,

I have a selectfield for choosing a childrenpage as linktarget I call this with  
`if($page->bookingbtntarget()->isNotEmpty()): echo $page->bookingbtntarget()->toPage()->url(); endif;`  
but I get the error

Call to a member function url() on boolean

I’m a little bit confused because I proofed this and the `->toPage()->url()`is the right way to get the whole URL for the link.  
If I delete the `toPage()`it work but I got a wrong build url.

This is the blueprint:

```
  hasbookingbtn:
    label: Zeige Buchenbutton mit Link auf Reservierungsseite
    text: Der Reservierungsbutton wird angezeigt wenn die Checkbox markiert ist.
    type: checkbox

  bookingbtntext:
    label: Buchenbuttontext
    type: text

  bookingbtntarget:
    label: Buchenbutton Link Ziel
    type: select
    default: reservierung
    options: query
    query:
      fetch: children
      value: '{{uid}}'

  bookingbtncolor:
    label: Buchenbutton Hintergundfarbe
    type: pickacolor

```

And the Snippet:

```
<?php

if($page->hasbookingbtn()->bool()): ?>
<div class="bookingcom col-xs-24 col-sm-24 col-md-24 col-ld-24">
    <a class="btn btn-red bookingbtn col-xs-20 col-sm-20 col-md-24 col-lg-24 ajaxlink" href="<?php if($page->bookingbtntarget()->isNotEmpty()): echo $page->bookingbtntarget()->toPage()->url(); endif; ?>" data-name="booking" <?php if(!$page->bookingbtncolor()->empty()): ?>style="background: <?php echo $page->bookingbtncolor()->value(); endif; ?>">
        <?php echo html($page->bookingbtntext()) ?>
    </a>
</div>
<?php endif; ?>

```

---

<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:** [April 14, 2018, 7:25pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/2 "2018-04-14T19:25:13Z")

</div>

Although I keep repeating it: Never call an object method without prior checking if the object exists. It’s not important if your field contains anything, it is important that the object - in this case the page - exists. This is necessary because at the time of saving, your page might exists. But what if it is deleted later, or renamed? Then you have a value in your field - but the page is gone and your `toPage()`method can’t create a page object. Then you end up with a broken page.

`Nough theory, let’s do it:

```auto
<?= ($p = $page->bookingbtntarget()->toPage())? $p->url():null; ?>

```

With this code, you don’t have to check if the field contains a value or not. If it is empty, you won’t have a page.

By the way: To avoid the problem with renamed pages, the AutoID plugin is a good choice.

---

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [April 14, 2018, 8:21pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/3 "2018-04-14T20:21:15Z")

</div>

Hello Sonja.

thank you for your explanation what I’m doing wrong!

This ternary operator is now ending with null. But the page exist and I can call it directly.  
To solve it for me now, because I have too much time invested to solve this problem I wrote it as  
`if($page->bookingbtntarget()->isNotEmpty()): echo $page->url() .'/'.$page->bookingbtntarget()->uid(); endif;`

I know this is really crap. But I need to get finish.

For me personally I’ll investigation further why the object is here not present.

---

<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:** [April 14, 2018, 8:23pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/4 "2018-04-14T20:23:36Z")

</div>

Maybe you saved the UID instead of the Uri? And if the page is a subpage, it won’t work.

---

<div class="post-metadata">

**Author:** ![JanStieler](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/janstieler/32/15642_2.png) [@JanStieler](https://forum.getkirby.com/u/JanStieler)\
**Post date:** [April 14, 2018, 8:32pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/5 "2018-04-14T20:32:51Z")

</div>

Hi,  
ja you are right. With this both I have a little bit problems to choose the right also in the past.

---

<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:28pm UTC](https://forum.getkirby.com/t/call-to-a-member-function-url-on-boolean-on-selectfield-for-url/10225/6 "2025-02-06T17:28:27Z")

</div>


