How to refer to another Collections or "Accounts"

I have a “posts” collection which in the “author” field i would like it to be a select and the options will come from my Cockpit existing “Accounts”.
Any suggestions on how can i achieve that?
Many thanks!

add a field with the field type account-link

That’s the answer, much appreciation!

Hi Artur,

I can’t seem to populate the account-link field, how can i achieve this?
Thank you for your time

1 Like

Hi there,

I have the same problem of [jingyao97]… is there a way to populate the account-link field?

Thanks a lot!
Alessandro

Running into this as well. I’m digging through the code to see if I can find a way to do this, but if @artur or anyone has another suggestion, that’d be awesome :slight_smile:

@jingyao97, @boxabrain, @burchaz

Please open a new thread if your problem is kind of related, but different. You can link to the related thread instead. Sometimes I’m bored and I scroll through the latest threads - but I don’t often click an threads, that have a solution already…

edit: @jingyao97 Sorry, of cause you authored the thread. Please ignore the sentence above.

But as always, there are multiple ways to achieve this goal.

  1. Do a second api request

https://example.com/api/cockpit/listUsers?token=xxtokenxx&filter[_id]=user_id

  1. Modify contents on an event in /config/bootstrap.php
<?php
// return account name instead of account _id when requesting
// collection via /api/collections/get/test2
// collection has a field named "account" with type "account-link"
if (COCKPIT_API_REQUEST) {

    $app->on('collections.find.after.test2', function($name, &$entries) {

        foreach ($entries as &$entry) {

            if (isset($entry['account'])) {
                // find account
                $account = $this->storage->findOne('cockpit/accounts', ['_id' => $entry['account']]);
                // return account name
                $entry['account'] = !empty($account['name']) ? $account['name'] : $account['user'];
                // return account array
                // $entry['account'] = $account; // caution: unset sensible data first!
            }

        }

    });

}
  1. Create a custom api endpoint

Use a similar logic like above to populate the account information.

Caution: Don’t return all account data directly or unset email, api_key, password and _reset_token before passing $entry['account'] = $account!