mekhtievrs/laravel-hydrator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mekhtievrs/laravel-hydrator

最新稳定版本:1.0

Composer 安装命令:

composer require mekhtievrs/laravel-hydrator

包简介

Package for the mapping and hydrating laravel models from data arrays and collections

README 文档

README

Installation

composer require mekhtievrs/hydrator

After the install package add the service provider class to config/app.php

'providers' => [
    Mekhtievrs\Hydrator\HydratorServiceProvider::class,
],

Usage

Generate data mapper

php artisan make:mapper MyCustomDataMapper

After that generator makes the mapper class, like

<?php

namespace App\Mappers;

use Mekhtievrs\Hydrator\DataMapperInterface;

class MyMapper implements DataMapperInterface
{

    public function transform(array $data) : array
    {
        return [
            // Map properties here
        ];
    }

}

Data mapping

// 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

// 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();

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-22