Looking to hire Laravel developers? Try LaraJobs
This package is not available.

laravel-hydrator maintained by mekhtievrs

Description
Package for the mapping and hydrating laravel models from data arrays and collections
Last update
2023/11/22 19:36 (dev-master)
License
Links
Downloads
13

Comments
comments powered by Disqus

Laravel mapping and hydration package

Installation

~~~bash composer require mekhtievrs/hydrator ~~~ After the install package add the service provider class to config/app.php ~~~php 'providers' => [ Mekhtievrs\Hydrator\HydratorServiceProvider::class, ], ~~~

Usage

Generate data mapper

~~~bash php artisan make:mapper MyCustomDataMapper ~~~ After that generator makes the mapper class, like ~~~php <?php namespace App\Mappers; use Mekhtievrs\Hydrator\DataMapperInterface; class MyMapper implements DataMapperInterface { public function transform(array $data) : array { return [ // Map properties here ]; } } ~~~

Data mapping

~~~php // Map data array $data = ['id' => 1, 'name' => 'Test 1']; $mapped_collection = hydrator($data)->with(new MyMapper)->hydrate(); // or $mapped_collection = hydrator($data, new MyMapper)->hydrate(); // Map array of data array $data = [ ['id' => 1, 'name' => 'Test 1'], ['id' => 2, 'name' => 'Test 2'], ]; $mapped_collection = hydrator($data)->with(new MyMapper)->hydrate(); ~~~

Data hydrate to models

~~~php // Map array of data array $data = [ ['id' => 1, 'name' => 'Test 1'], ['id' => 2, 'name' => 'Test 2'], ]; $mapped_collection = hydrator($data)->with(new MyMapper)->to(User::class)->hydrate(); ~~~