# Update pages-field programatically

**URL:** <https://forum.getkirby.com/t/update-pages-field-programatically/17697>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 26, 2020, 7:50pm UTC](https://forum.getkirby.com/t/update-pages-field-programatically/17697 "2020-03-26T19:50:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonasfeige](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonasfeige/32/4550_2.png) [@jonasfeige](https://forum.getkirby.com/u/jonasfeige)\
**Post date:** [March 26, 2020, 7:50pm UTC](https://forum.getkirby.com/t/update-pages-field-programatically/17697/1 "2020-03-26T19:50:18Z")

</div>

Is there a way to update a pages field programmatically?  
I want to give users the opportunity to mark pages as their favourites in the frontend. The favourites should be saved in a pages field in the user’s profile via an AJAX-call to to a route.

```
$user->update([
  'favourites' => $conversation
]);

```

I’ve got this working as long as `$conversation` is a page object but not when I add a pages object.  
And to make things even more complicated: How would I save the previous favourites in order to add to them?

This is my (messy) route:

```
'pattern' => '/conversation/favourite/(:any)',
        'action' => function ($value) use ($kirby) {

          $data = Str::split($value, '+');
          $user = $kirby->user($data[0]);
          $conversation = $kirby->site()->find('conversations')->children()->findBy('uid', $data[1]);

          try {

            $user->update([
              'favourites' => $conversation
            ]);

            echo 'The user has been updated';
          } catch (Exception $e) {

            echo 'The user could not be updated';
            // optional reason: echo $e->getMessage();

          }
          return ' ';
        }

```

---

<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 26, 2020, 8:01pm UTC](https://forum.getkirby.com/t/update-pages-field-programatically/17697/2 "2020-03-26T20:01:05Z")

</div>

The pages field stores a list of page ids in this format:

```auto
pages:
- path/to/page
- path/to/another/page

```

So what you need to do, is read the original content into an array, using `yaml()`, then add any new items to this array, then store again as `yaml` encoded array.

```auto
Data::encode($array, 'yaml');

```

---

<div class="post-metadata">

**Author:** ![jonasfeige](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonasfeige/32/4550_2.png) [@jonasfeige](https://forum.getkirby.com/u/jonasfeige)\
**Post date:** [March 26, 2020, 8:07pm UTC](https://forum.getkirby.com/t/update-pages-field-programatically/17697/3 "2020-03-26T20:07:36Z")

</div>

Ah, perfect. Would have never figured that out. Will give it a try tomorrow.  
Thanks so much!

---

<div class="post-metadata">

**Author:** ![jonasfeige](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jonasfeige/32/4550_2.png) [@jonasfeige](https://forum.getkirby.com/u/jonasfeige)\
**Post date:** [March 27, 2020, 7:04am UTC](https://forum.getkirby.com/t/update-pages-field-programatically/17697/4 "2020-03-27T07:04:39Z")

</div>

Thanks again @texnixe, I got this working really quickly this way. Just a note for future readers, I think the method needs to be` yaml()`, not `toYaml()`.
