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 ' ';
}