PHPackages                             schulzefelix/laravel-data-transfer-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. schulzefelix/laravel-data-transfer-object

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

schulzefelix/laravel-data-transfer-object
=========================================

Data Transfer Objects with Attribute Casting and Date Mutators.

1.5.2(4y ago)843.7k↓31.6%38MITPHPPHP ^8.0.2

Since Nov 22Pushed 2y ago2 watchersCompare

[ Source](https://github.com/schulzefelix/laravel-data-transfer-object)[ Packagist](https://packagist.org/packages/schulzefelix/laravel-data-transfer-object)[ Docs](https://github.com/schulzefelix/laravel-data-transfer-object)[ RSS](/packages/schulzefelix-laravel-data-transfer-object/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (11)Used By (8)

Laravel Data Transfer Objects
=============================

[](#laravel-data-transfer-objects)

[![Latest Version](https://camo.githubusercontent.com/9b98645312a682179db8f765e24c85604ba20d227749eacc80f8473de419201a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f736368756c7a6566656c69782f6c61726176656c2d646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://github.com/schulzefelix/laravel-data-transfer-object/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/637302afecc7b62c461d2c53d2569c00767f3a5063e8e84403c5a7abff5ede53/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736368756c7a6566656c69782f6c61726176656c2d646174612d7472616e736665722d6f626a6563742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/schulzefelix/laravel-data-transfer-object)[![StyleCI](https://camo.githubusercontent.com/72f24545477218f3c94ccf1f8bdc1110ca1dad4a6da74057e5cd4a5dde8ec5c6/68747470733a2f2f7374796c6563692e696f2f7265706f732f37343438383137312f736869656c64)](https://styleci.io/repos/74488171)[![Latest Version on Packagist](https://camo.githubusercontent.com/e7ed554cb304da1ecbb636aafc9415cb7e687275836a28697bd0416742a138fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736368756c7a6566656c69782f6c61726176656c2d646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/schulzefelix/laravel-data-transfer-object)[![Total Downloads](https://camo.githubusercontent.com/2a6c5eff5ea745c5c2ecb6737a38d439213107a4d5faf7cce720cc63b025f9c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736368756c7a6566656c69782f6c61726176656c2d646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/schulzefelix/laravel-data-transfer-object)

Brings Laravel Attribute Casting and Date Mutators to objects.

Install
-------

[](#install)

Via Composer

```
$ composer require schulzefelix/laravel-data-transfer-object
```

Usage
-----

[](#usage)

- [Introduction](#introduction)
- [Accessors &amp; Mutators](#accessors-and-mutators)
    - [Defining An Accessor](#defining-an-accessor)
    - [Defining A Mutator](#defining-a-mutator)
- [Date Mutators](#date-mutators)
- [Attribute Casting](#attribute-casting)
- [Serializing Objects &amp; Collections](#serializing-objects-and-collections)
    - [Serializing To Arrays](#serializing-to-arrays)
    - [Serializing To JSON](#serializing-to-json)

Introduction
------------

[](#introduction)

Accessors and mutators allow you to format attribute values when you retrieve or set them on object instances.

In addition to custom accessors and mutators, it can also automatically cast date fields to [Carbon](https://github.com/briannesbitt/Carbon) instances.

Accessors &amp; Mutators
------------------------

[](#accessors--mutators)

### Defining An Accessor

[](#defining-an-accessor)

To define an accessor, create a `getFooAttribute` method on your object where `Foo` is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the `first_name` attribute. The accessor will automatically be called when attempting to retrieve the value of the `slug` attribute:

```
