> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/home/laravel-data-mapper/getting-started/usage.md).

# Usage

Data transfer objects are useful to pass data, they can be used everywhere but has some special uses in multiple places like controllers (including their route bindings) and queued jobs.

In case of using the mapper there is a namespaced function to be used to simplify its usage:&#x20;

```php
use Carbon\Carbon;

use function OpenSoutheners\LaravelDataMapper\map;

$mappedValue = map(1747939147)->to(Carbon::class);
```

## Mappers

These capabilities are called internally as mappers and they can be used to [extend the functionalities of this mapper in 2 different ways](/home/laravel-data-mapper/advanced/extending-mapper.md).

These are the currently built-in mappers:

* Collections: Map delimited lists strings or arrays of data into [Laravel collections](https://laravel.com/docs/collections).
* Objects: Map JSON strings or arrays of data into class objects.
* Generic objects: Map JSON strings or arrays of data into [`\stdClass`](https://www.php.net/manual/en/class.stdclass.php) objects.
* [Carbon](https://carbon.nesbot.com/docs/): Map UNIX numeric timestamps or timestamps in different formats to a Carbon instance.
* [Backed enums](https://www.php.net/manual/en/language.enumerations.backed.php): Map string or integer to a backed enum case.
* Models: Map delimited lists strings or string/numeric keys to an [Eloquent model](https://laravel.com/docs/eloquent) instance.

### Collections

```php
use Illuminate\Support\Collection;

map(['Ruben', 'Taylor'])->to(Collection::class);
map('Ruben, Taylor')->to(Collection::class);
map('Ruben', 'Taylor')->to(Collection::class);
```

### Objects

{% hint style="info" %}
This topic is extended in the [following page](/home/laravel-data-mapper/data-objects.md).
{% endhint %}

```php
class UserObject
{
    public function __construct(public string $name, public string $surname) {}
}

map(['name' => 'Taylor', 'surname' => 'Otwell'])->to(UserObject::class);
map('{"name": "Taylor", "surname": "Otwell"}')->to(UserObject::class);
```

### Generic objects

```php
map(['name' => 'Taylor', 'surname' => 'Otwell'])->to(\stdClass::class);
```

### Carbon

```php
use Illuminate\Support\Carbon;

map(1747939147)->to(Carbon::class);
map('1747939147')->to(Carbon::class);
map('2025-05-22')->to(Carbon::class);
```

### Backed enums

```php
enum UserStatus: int
{
    case Active = 1;
    
    case Inactive = 2;
    
    case Banned = 3;
}

map(1)->to(UserStatus::class);
map('2')->to(UserStatus::class);
```

### Models

```php
use App\Models\User;

map(1)->to(User::class);
map(1, 2)->to(User::class);
```

## Mapping through

In some contexts we probably want a collection of mapped data results from the mapper, in this cases there is the `through` method, in the following it will return a `Collection` of `Carbon` instances:

```php
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

map(1747939147, 1781934121)->through(Collection::class)->to(Carbon::class);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.opensoutheners.com/home/laravel-data-mapper/getting-started/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
