疯狂小舸/laravel-mapped-model-fields

此包的最新版本(1.0.0)没有可用的许可证信息。

将 Eloquent 构建器和关系方法添加到创建和更新之前已映射字段的模型

1.0.0 2019-03-14 08:29 UTC

This package is auto-updated.

Last update: 2024-09-14 21:25:42 UTC


README

安装

您可以通过 composer 安装此包

composer require crazybooot/laravel-mapped-model-fields

用法

通过在模型类中添加 public function getFieldsMap(): array 方法实现 Crazybooot\MappedModelFields\Contracts\HasMappedFields 接口。此方法应返回关联数组,其中键代表当前模型的目标属性,值是映射到源数组的字符串路径。您可以使用点表示法从嵌套源数组映射值。

<?php
declare(strict_types = 1);

namespace App\Models;

use Crazybooot\MappedModelFields\Contracts\HasMappedFields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * Class Product
 *
 * @package App\Models
 */
class Product extends Model implements HasMappedFields
{
        /**
         * @return array
         */
        public function getFieldsMap(): array
        {
            return [
                'vendor_id'         => 'identifier',
                'title'             => 'information.title',
                'vendor_created_at' => 'created_at',
            ];
        }
}

之后,您可以从外部源创建或更新具有映射的模型

$externalProduct = [
    'identifier'        => 443765834,
    'information'     => [
        'title' => 'Some awesome product',
    ],
    'crated_at' => '2018-12-21 14:36:04',
];
//model method to map values from source and create entitie
$product = Product::mapAndCreate($externalProduct);

//relation method to map values from source and create related entitie
$product = $user->products()->mapAndCreate($externalProduct);

//model method to map values from source and update or create entitie
$product = Product::mapAndUpdateOrCreate(
    ['vendor_id' => $externalProduct['identifier']], 
    $externalProduct
);

//relation method to map values from source and update or create related entitie
$product = $user
    ->products()
    ->mapAndUpdateOrCreate(
        ['vendor_id' => $externalProduct['identifier']], 
        $externalProduct
    );
    
//model method to map values and update enitie
$product->mapAndUpdate($externalProduct);

方法 mapAndUpdatemapAndCreate 接受一个值数组,这些值将作为映射后的第二个参数追加到属性数组中,以及一个键数组,这些键将在映射后将从属性数组中排除。 mapAndUpdateOrCreate 方法分别接受追加和排除数组作为其第三和第四个参数。

$externalProduct = [
    'identifier'        => 443765834,
    'information'     => [
        'title' => 'Some awesome product',
    ],
    'crated_at' => '2018-12-21 14:36:04',
];
//append value to resulting array after mapping
$product = Product::mapAndCreate($externalProduct, ['user_id' => $user->getKey()]);
    
//axclude specified source value from mapping
$product->mapAndUpdate($externalProduct, [], ['information.title']);

您可以指定映射数据的转换

        /**
         * @return array
         */
        public function getFieldsMap(): array
        {
            return [
                'vendor_id'         => 'identifier',
                'title'             => [
                    'key'       => 'informaion.title',
                    'transform' => function($value) {
                        return str_upper($value);
                    }
                ],
                'vendor_created_at' => 'created_at',
            ];
        }

许可证

MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。