Laravel Data Object Tools

juststeveking/laravel-data-object-tools image

Laravel Data Object Tools stats

Downloads
4.7K
Stars
59
Open Issues
0
Forks
6

View on GitHub →

A set of tools to make working with Data Transfer Objects easier in Laravel

Laravel Data Object Tools

This package is aimed to be a suite of tools to help working with DTOs even easier. It includes an artisan command to generate DTOs as well as a facade to help you hydrate them.

Installation

composer require juststeveking/laravel-data-object-tools

Usage

To generate a new DTO all you need to do is run the following artisan command:

php artisan make:dto MyDto

This will generate the following class: app/DataObjects/MyDto.php. By default this class will be a final class that implements a DataObjectContract which enforces a method toArray so that you can easily cast your DTOs to arrays.

If you are using PHP 8.2 however, you will by default get a readonly class generated, so that you do not have to declare each property as readonly.

To work with the hydration functionality you can either use Laravels DI container, or the ready made facade.

Using the container:

class StoreController
{
public function __construct(
private readonly HydratorContract $hydrator,
) {}
 
public function __invoke(StoreRequest $request)
{
$model = Model::query()->create(
attributes: $this->hydrator->fill(
class: ModelObject::class,
parameters: $request->validated(),
)->toArray(),
);
}
}

To work with the facade, you can do something very similar:

class StoreController
{
public function __invoke(StoreRequest $request)
{
$model = Model::query()->create(
attributes: Hydrator::fill(
class: ModelObject::class,
parameters: $request->validated(),
)->toArray(),
);
}
}

Object Hydration

Under the hood this package uses an EventSauce package, created by Frank de Jonge. It is possibly the best package I have found to hydrate objects nicely in PHP. You can find the documentation here if you would like to see what else you are able to do with the package to suit your needs.

Testing

To run the test suite:

composer run test

Credits

LICENSE

The MIT License (MIT). Please see License File for more information.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Juststeveking Laravel Data Object Tools Related Articles

Building your own Laravel Packages image

Building your own Laravel Packages

Read article
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic