# PHP get contact with associations

**URL:** <https://community.hubspot.com/t/php-get-contact-with-associations/88227>\
**Category:** APIs & Integrations\
**Tags:** contacts\
**Created:** [August 24, 2023, 9:03am UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227 "2023-08-24T09:03:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssulinski](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@ssulinski](https://community.hubspot.com/u/ssulinski)\
**Post date:** [August 24, 2023, 9:03am UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227/1 "2023-08-24T09:03:38Z")

</div>

Is there a way to retrieve associations for a given contact with a single call and if not - how would I obtain associations for a given contact with a separate call? I cannot seem to be able to find any resource that would show that.

Currently I’m getting the the contact using one of the examples from the API documentation:

```lia-code-sample
$filter = new Filter;

$filter->setOperator(Filter::OPERATOR_EQ)
 ->setPropertyName('email')
 ->setValue($email);

$filterGroup = new FilterGroup;
$filterGroup->setFilters([$filter]);

$searchRequest = new PublicObjectSearchRequest;
$searchRequest->setFilterGroups([$filterGroup]);

$searchRequest->setProperties([
 'firstname',
 'lastname',
 'email',
 'company',
 'website',
]);

$response = $this->discovery
 ->crm()
 ->contacts()
 ->searchApi()
 ->doSearch($searchRequest);

if (!$response->valid()) {
 throw new ErrorException(
 $response?->getMessage() ?? 'Api response returned error'
 );
}

return $response->getResults()[0]->getProperties();

```

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [August 25, 2023, 11:47am UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227/2 "2023-08-25T11:47:49Z")

</div>

Hello @ssulinski  
Right now it’s not possible to fetch an association with the contact within the single API call, first, run an API to fetch the contact and then run an API call to fetch the associated object.

---

<div class="post-metadata">

**Author:** ![ssulinski](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@ssulinski](https://community.hubspot.com/u/ssulinski)\
**Post date:** [September 7, 2023, 5:37am UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227/3 "2023-09-07T05:37:22Z")

</div>

Thank you @himanshurauthan

---

<div class="post-metadata">

**Author:** ![KPadhiyar](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@KPadhiyar](https://community.hubspot.com/u/KPadhiyar)\
**Post date:** [August 27, 2024, 9:16am UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227/4 "2024-08-27T09:16:40Z")

</div>

@ssulinskiIf possible, could you please share your code on how did you get contact with the association? I’m also working on same things.

---

<div class="post-metadata">

**Author:** ![ssulinski](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@ssulinski](https://community.hubspot.com/u/ssulinski)\
**Post date:** [August 27, 2024, 1:48pm UTC](https://community.hubspot.com/t/php-get-contact-with-associations/88227/5 "2024-08-27T13:48:42Z")

</div>

@KPadhiyar here’s the method I’ve created to obtain company associations for a given contact id (I’m using Laravel and its Collection in this example):

```lia-code-sample
public function getContactToCompanyAssociations(int $contact_id): Collection
{
 $response = $this->discovery
 ->crm()
 ->associations()
 ->v4()
 ->batchApi()
 ->getPage(
 from_object_type: '0-1',
 to_object_type: '0-2',
 batch_input_public_fetch_associations_batch_request: new BatchInputPublicFetchAssociationsBatchRequest([
 'inputs' => [['id' => $contact_id]],
 ])
 );

 if (! $response->valid()) {
 throw new ErrorException(
 $response?->getMessage() ?? 'Api response returned error'
 );
 }

 if (! $results = $response->getResults()) {
 return new Collection;
 }

 return collect($results)
 ->pluck('to')
 ->flatMap(function (array $companies) {
 return array_map(
 fn (MultiAssociatedObjectWithLabel $company) => $company->getToObjectId(),
 $companies
 );
 });
}

```

The `discovery` property is the constructor injected instance of `HubSpot\Discovery\Discovery`
