Update collection


#1

Hello people

Well cockpit is great. I realy like the simplicity. I tried some things out but could find any answer to it.

According to the docs can create/update collection entries from frontend.

.fetch('/api/collections/save/posts?token=xxtokenxx', {
  method: 'post',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    data: {...}
  })
})
.then(res=>res.json())
.then(entry => console.log(entry));

Well I was able to create new entries but couldnt find out how do UPDATE existing entries. The code above only creates new ones. Does anyone have a clue how to do that? And is it even possible to update singletons from frontend?

Ah, and is there a way to contribute to the documentation? Like help them creating better updated docs?

thanks.


#2

The only difference in usage between update and insert is the fact that you pass an ID along or not.

If you post with the “_id” field set in your data, cockpit will update the data determined by the id you gave; if you post without the “_id” field cockpit will create a fresh new entry.


If you’d like to contribute to the documentation, create a PR here: https://github.com/agentejo/cockpit-docs


#3

Thank you very much for your reply! Will try it as soon I can. Is there a way to update singletons as well from frontend? I do not see like /singletons/save in the api.

And thanks for the link with the git for the docs.


#4

the api method to call is “update_data”:

you call it this way: http://your-domain.com/singletons/update_data/my_pretty_cool_singleton_name


#5

If you want to save singleton data via api, just create a custom api endpoint in
config/singletons/save/singletonname.php with this content:

<?php

return $this->invoke('Singletons\\Controller\\Admin', 'update_data', ['singletonname']);

Now send a request:
https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx

json post body:
{"data":{"title":"test","content":"updated content"}}

Caution: It updates empty values, too. So you always have to send all fields or the other fields will be deleted.