定制 mindtwo/laravel-dynamic-model-mutators 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

mindtwo/laravel-dynamic-model-mutators

Composer 安装命令:

composer require mindtwo/laravel-dynamic-model-mutators

包简介

Laravel Dynamic Model Mutators

README 文档

README

Tests Latest Stable Version Total Downloads MIT Software License

Important Notice:

The following documentation refers to packacke version 1.x
We will add documentation for version 2.x as soon as possible. Sorry.

Introduction

This package is an extension for Laravel's eloquent model. It allows you to define multiple get and set mutators by registering your own callback functions. This is a simple way to inject Laravel's getAttribute() and setAttribute() methods, especially within different traits on a single model.

Installation

You can install the package via composer:

composer require mindtwo/laravel-dynamic-model-mutators

How to use?

Use the "DynamicModelMutator" trait in your eloquent models:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;

class User extends Model
{
    use DynamicModelMutator;
}

In the boot method of the model you can now register the dynamic getter and setter functions e.g.:

/**
 * The "booting" method of the model.
 *
 * @return void
 */
protected static function boot()
{
    static::registerSetMutator('translations', 'setAttributeTranslation');
    static::registerGetMutator('translations', 'getAttributeTranslation');
}

registerSetMutator and registerGetMutator expecting two parameters. First is name of mutator, which is the name of the property which configures your attributes for the registered mutator, too. The second is the name of the callback function which will be called on the same object.

Defining attributes for mutators

To define attributes for your mutators you have to set up a class property with the mutator key as its name. For example we use 'translations' as mutator key, so we can setup our attributes by defining an $translations property on our model.

namespace Examples;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface; 

class exampleModel extends Model implements DynamicModelMutatorInterface 
{
    use use DynamicModelMutator;
    
    $translations = [
        'attribute_1',
        'attribute_2',
        'attribute_3'
    ];
}

Optionally you can set up a attribute based configuration which will be passed to you mutator methods. So you can add additional configuration to your attributes.

namespace Examples;

use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDynamicModelMutators\DynamicModelMutator;
use mindtwo\LaravelDynamicModelMutators\Interfaces\DynamicModelMutatorInterface; 

class exampleModel extends Model implements DynamicModelMutatorInterface 
{
    use use DynamicModelMutator;
    
    $translations = [
        'attribute_1' = 'string',
        'attribute_2' = 'text',
        'attribute_3' = [
            'type'    => string,
            'locales' => ['en', 'de'] 
        ]
    ];
}

Set mutator callback function

The callback function for a set mutators must accept three arguments. First is the attribute name, second is the attribute value, third is the attribute specific configuration. Because they will be called automatically within the trait, there is no need to return any value.

/**
 * Set attribute translation.
 *
 * @return void
 */
public function setAttributeTranslation($name, $value, $config=null)
{
    // Set value $value for attribute $name 
}

Get mutator callback function

The callback function for a get mutators must accept two arguments. First is the attribute name, second is the attribute specific configuration. It should return the attribute value for a given attribute name. Note, that you can not use Laravel's attribute casting feature for a dynamic mutated attribute, but it's quite easy to implement our own castings.

/**
 * Get attribute translation.
 *
 * @return void
 */
public function setAttributeTranslation($name, $config=null)
{
    // Get value for attribute $name 
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email info@mindtwo.de instead of using the issue tracker.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固