Update collection

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.

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

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.

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

If you want to save singleton data via api, just create a custom api endpoint in
config/api/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.


edit: fixed wrong folder name of custom endpoint - Thanks @SimonHayden for the correction

There is an error in @raffaelj’s, but otherwise is correct.

The php file singletonname.php should be in config/api/singletons/save/singletonname.php, which would then create an endpoint to save to Singleton “singletonname” at https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx

The API endpoint won’t work when not in api folder.

Equally, one could also put singletonname.php in config/api/singletonname.php and then POST data to https://url.to/cockpit/api/singletonname?token=xxtokenxx.