# Pages field with database connection

**URL:** <https://forum.getkirby.com/t/pages-field-with-database-connection/25271>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 15, 2022, 11:08am UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271 "2022-04-15T11:08:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![quentin-f451](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/quentin-f451/32/6730_2.png) [@quentin-f451](https://forum.getkirby.com/u/quentin-f451)\
**Post date:** [April 15, 2022, 11:08am UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/1 "2022-04-15T11:08:37Z")

</div>

Hi there,

I’m currently trying to build a website with a connection to a MySQL database. Thanks to the few courses found on the website, the whole thing is working quite well and I’m reading and writing the content of the pages in a very handy way through the database.

Though, I can’t figure out how to work with the Pages field in that case. I have tables in my DB for many-to-many relationships that makes it quite easy to retrieve the pages I want (for example, the artists for an artwork). But I can’t manage to find the way to show those pages in a Pages field on the back-office interface.

I tried with Pages collections and arrays of pages IDs, but none of those two solutions happened to work.

Am I missing something here?

Thanks in advance!

---

<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 15, 2022, 11:14am UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/2 "2022-04-15T11:14:06Z")

</div>

Do these related pages actually exist in your Kirby installation? How are they stored? The pages field only works with either “real” (=exist in file system) pages or virtual pages, but for virtual related pages you would also need models.

---

<div class="post-metadata">

**Author:** ![quentin-f451](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/quentin-f451/32/6730_2.png) [@quentin-f451](https://forum.getkirby.com/u/quentin-f451)\
**Post date:** [April 15, 2022, 11:25am UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/3 "2022-04-15T11:25:35Z")

</div>

These related pages are virtual pages created from the database too with a page model, yes. So they have a page ID, a slug, and I do retrieve them as Page when I get the data on the front-end. But I can’t find a way to show them as a list of related pages in a Pages field in the back-office.

---

<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 15, 2022, 11:54am UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/4 "2022-04-15T11:54:14Z")

</div>

What does your model that included the related pages currently look like?

To populate the pages field, you have to pass an array of page ids as value.

---

<div class="post-metadata">

**Author:** ![quentin-f451](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/quentin-f451/32/6730_2.png) [@quentin-f451](https://forum.getkirby.com/u/quentin-f451)\
**Post date:** [April 15, 2022, 12:53pm UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/5 "2022-04-15T12:53:29Z")

</div>

Well, unfortunately, I can’t make it work by passing an array of page IDs.

This is the model for the parent page:

```php
<?php
class WorksPage extends Page
{
  public function children()
  {
    $works = [];
    foreach (Db::select('work') as $work) {
      $works[] = [
        'slug' => $work->slug(),
        'num' => $work->status() === 'listed' ? 0 : null,
        'template' => 'work',
        'model' => 'work',
        'content' => [
          'db_id' => $work->id(),
          'title' => $work->title(),
          'status' => is_null($work->status()) ? 'draft' : $work->status(),
          'artists' => $work->artists()
        ]
      ];
    }

    return Pages::factory($works, $this);
  }
}

```

This is the `artists()` function in the child model that create the array of IDs from the database:

```php
  public function artists()
  {
    $connections = Db::select('intervention', 'artist_id', 'work_id = ' . $this->db_id())->values();
    $artists = collection('artists')->filterBy(function ($artist) use ($connections) {
      return in_array(strval($artist->db_id()), array_column($connections, 'artist_id'));
    })->map(function ($artist) {
      return $artist->id();
    })->values();
    return $artists;
  }

```

When I print `$page->artists()` on a child page, I do have an array of page IDs. But the `artists` Pages field on the back office still looks empty.

---

<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 15, 2022, 12:57pm UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/6 "2022-04-15T12:57:15Z")

</div>

What does the blueprint definition for the artists field look like?

---

<div class="post-metadata">

**Author:** ![quentin-f451](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/quentin-f451/32/6730_2.png) [@quentin-f451](https://forum.getkirby.com/u/quentin-f451)\
**Post date:** [April 15, 2022, 1:08pm UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/7 "2022-04-15T13:08:49Z")

</div>

Nothing in particular, I removed every unnecessary property to avoid any problem at the moment.

```nohighlight
fields:
  artists:
    label: Artistes
    type: pages

```

---

<div class="post-metadata">

**Author:** ![quentin-f451](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/quentin-f451/32/6730_2.png) [@quentin-f451](https://forum.getkirby.com/u/quentin-f451)\
**Post date:** [April 15, 2022, 1:10pm UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/8 "2022-04-15T13:10:18Z")

</div>

But I just understood where the problem is. I actually call the `artists()` function from the child model in the parent model before the element from the array is transformed into a page. So it can’t work, obviously!  
Thanks for your help!

---

<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 15, 2022, 1:20pm UTC](https://forum.getkirby.com/t/pages-field-with-database-connection/25271/9 "2022-04-15T13:20:28Z")

</div>

But your field should also query the children of the `artists` page, not of the current page.
