/**
* Mapping class extending the base mapping class
*/
class ExampleExtendMap extends MapCreateContact
{
public function map(Enumerable $data): array
{
return array_merge(
parent::map(data: $data),
['foo' => 'bar']
);
}
}
/**
* Controller action
*/
Route::get('create-contact-extend-map', function () {
$data = collect(value: [
'email' => '[email protected]',
'first_name' => 'John',
'last_name' => 'Doe',
]);
$createContact = new CreateContact();
$response = $createContact
->mapUsing(class: ExampleExtendMap::class)
->execute(data: $data);
return $response->json();
});
/**
* Custom mapping class
*/
class ExampleCustomMap implements Mapper
{
public function map(Enumerable $data): array
{
return [
'foo' => 'bar'
];
}
}
/**
* Controller action
*/
Route::get('create-contact-custom-map', function () {
$data = collect(value: [
'email' => '[email protected]',
'first_name' => 'John',
'last_name' => 'Doe',
]);
$createContact = new CreateContact();
$response = $createContact
->mapUsing(class: ExampleCustomMap::class)
->execute(data: $data);
return $response->json();
});
Getting custom fields of a resource is determined by the resource name. The convention used to determine the correct resource is the navigation title on the left side at the documentation page lowercased.
Custom fields is only supported for resources that support custom fields
Resource | navigation title |
---|---|
accounts | ACCOUNTS |
contacts | CONTACTS |
deals | DEALS |
/**
* Custom mapping class with custom fields
*/
class ExampleMappingWithCustomFields implements Mapper
{
use HasCustomFields;
/**
* @throws \JobVerplanke\LaravelActiveCampaign\Exceptions\ResourceException
* @throws \Illuminate\Http\Client\RequestException
*/
public function map(Enumerable $data): array
{
$fields = $this->getCustomFieldsFor(resource: 'contacts');
return [
'foo' => 'bar',
'custom_field' => $fields->get(key: 'PERSTAG')
];
}
}
/**
* Controller action
*/
Route::get('create-contact-custom-map-with-custom-fields', function () {
$data = collect(value: [
'email' => '[email protected]',
'first_name' => 'John',
'last_name' => 'Doe',
]);
$createContact = new CreateContact();
$response = $createContact
->mapUsing(class: ExampleMappingWithCustomFields::class)
->execute(data: $data);
return $response->json();
});