# How to refer to another Collections or "Accounts"

**URL:** <https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324>\
**Category:** Uncategorized\
**Created:** [October 9, 2018, 9:12pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324 "2018-10-09T21:12:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jingyao97](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/jingyao97/32/127_2.png) [@jingyao97](https://discourse.getcockpit.com/u/jingyao97)\
**Post date:** [October 9, 2018, 9:12pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/1 "2018-10-09T21:12:32Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [October 9, 2018, 11:32pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/2 "2018-10-09T23:32:37Z")

</div>

add a field with the field type `account-link`

---

<div class="post-metadata">

**Author:** ![jingyao97](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/jingyao97/32/127_2.png) [@jingyao97](https://discourse.getcockpit.com/u/jingyao97)\
**Post date:** [October 10, 2018, 3:43pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/3 "2018-10-10T15:43:17Z")

</div>

> [@artur](#):
>
> account-link

That’s the answer, much appreciation!

---

<div class="post-metadata">

**Author:** ![jingyao97](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/jingyao97/32/127_2.png) [@jingyao97](https://discourse.getcockpit.com/u/jingyao97)\
**Post date:** [October 11, 2018, 5:37pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/4 "2018-10-11T17:37:16Z")

</div>

Hi Artur,

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

---

<div class="post-metadata">

**Author:** ![boxabrain](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/boxabrain/32/207_2.png) [@boxabrain](https://discourse.getcockpit.com/u/boxabrain)\
**Post date:** [January 9, 2019, 6:41pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/5 "2019-01-09T18:41:38Z")

</div>

Hi there,

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

Thanks a lot!  
Alessandro

---

<div class="post-metadata">

**Author:** ![burchaz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/burchaz/32/445_2.png) [@burchaz](https://discourse.getcockpit.com/u/burchaz)\
**Post date:** [November 22, 2019, 12:02am UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/6 "2019-11-22T00:02:33Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 22, 2019, 6:33pm UTC](https://discourse.getcockpit.com/t/how-to-refer-to-another-collections-or-accounts/324/7 "2019-11-22T18:33:50Z")

</div>

@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
<?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

> [@How to create custom API Endpoints](https://discourse.getcockpit.com/t/how-to-create-custom-api-endpoints/202/5):
>
> In Cockpit you can create a custom api entry point pretty easy: Create the following file /config/api/custom.php we now do a custom query in that file: \<?php // very simple code here $test = $this-\>param('test'); if (!$test) { return false; } return $test; now you can query /api/custom?token=xxx&test=foo,bar,baz if you want to have the api public (without token) then create the file in a subfolder named public: /config/api/public/custom.php now you can query /api/public/custom?tes…

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`!
