定制 fhteam/eloquent-custom-attributes 二次开发

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

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

fhteam/eloquent-custom-attributes

Composer 安装命令:

composer require fhteam/eloquent-custom-attributes

包简介

Custom attribute handling for Laravel's Eloquent ORM

README 文档

README

This small library allows you to define custom (virtual) attributes on Eloquent models and use them to interact (or not, if you so choose) with real ones.

Installation

  • Simple composer installation is ok: composer require fhteam/eloquent-custom-attributes:dev-master (set version requirement to your favourite)

Usage

  • Add CustomAttributeHelperTrait to your model and any number of various attribute handler traits to your model
  • Define $customAttributes property and list there attribute names together with classes, responsible for their handling
  • Override Eloquent's getAttribute and setAttribute methods so that they work with yout custom attributes
  • After that work with your attribute just you are used to. See the example below
/**
 * @property array $json_meta_data This attribute is actually of 'string' 
 *                                 type in database and in model. 
 *                                 But we can work with it just like 
 *                                 it is array delegating all underlying 
 *                                 operations to JsonAttrHandlerTrait
 */
class Invoice extends Eloquent
{
    use CustomAttributeHelperTrait;
    use JsonAttrHandlerTrait;

    protected $customAttributes = [
        'json_meta_data' => JsonAttrHandlerTrait::class,
    ];

    /**
     * We have to override Eloquent's getAttribute() /  
     * setAttribute() on the model directly (not in CustomAttributeHelperTrait) 
     * because you might wish to do something in those methods yourself
     */
    public function getAttribute($key)
    {
        if ($this->isCustomAttribute($key)) {
            return $this->getCustomAttribute($key);
        }

        return parent::getAttribute($key);
    }

    public function setAttribute($key, $value)
    {
        if ($this->isCustomAttribute($key)) {
            $this->setCustomAttribute($key, $value);

            return;
        }

        parent::setAttribute($key, $value);
    }
    
/**
 * Just some simple class to show actual work with model ;)
 */
class Main
{
    public function main() {
        $invoice = Invoice::findOrFail(1);
        $invoice->json_meta_data = ['key' => 'value'];
        $invoice->json_meta_data['key_next'] = 'value_next';
        
        $invoice->save();
    }
}

Interesting

  • The name of the custom attribute can be the same as the name of the real attribute of the Laravel model. In this case custom attribute will hide real one and then the interaction with it will be available via responsible trait only (well, you can still access raw attribute values in the $model->attributes[] though). But of course you can use another name for a custom attribute to interact with a real one if you need them both to be visible
  • You can have one custom attribute (date attribute for example) to interact to several underlying real attributes of the model (day, month and year, separately if you wish)

Extending

Should you want to implement your own custom attribute handler, you have just to implement the trait, responsible for value conversion

  • Naming convention for such traits is <typeName>AttrHandlerTrait
  • Attribute handling traits must implement two methods: handleGetAttribute<typeName> and handleSetAttribute<typeName>. Check Json\JsonAttrHandlerTrait for example
  • Be careful with method naming in traits. Don't make them too common since they must nicely co-exist with methods of other traits Eloquent model may use
  • If your custom attribute represents complex type (not just a string or a number) consider making a wrapper around it. An example of such wrapper can be ArrayAttributeWrapper. It encapsulates an array value, provides access to it and updates corresponding model whenever attribute changes. Wrapper is cached so attribute can be accessed several times from any part of the script

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 5
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2015-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固