定制 mrgrain/eloquent-service-injection 二次开发

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

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

mrgrain/eloquent-service-injection

最新稳定版本:1.1.0

Composer 安装命令:

composer require mrgrain/eloquent-service-injection

包简介

Simple trait to inject services into Eloquent models via a property.

README 文档

README

Simple trait to inject services into Eloquent models via a property.

Build Status Latest Stable Version Total Downloads License

Laravel's Eloquent models do not support constructor service injection. This trait aims to provide a simple and unified way of injection services into Eloquent models. It is doing that by using a property to define services, in a similar way how other model options can be defined (think $casts or $with for attribute casting and eager loading).

Requirements

  • PHP >= 5.6.0
  • Laravel >= 5.1 or Lumen >= 5.1

Installation

Add the package to your repository with Composer.

composer require mrgrain/eloquent-service-injection

Usage

Extend your models by using the trait:

namespace Mrgrain\EloquentServiceInjection;

class Comment extends Model {
    use ServiceInjectionTrait;
}

To always include the trait, add it to a base model all your models are extending from.

Add the services to be injected into the $inject property.

class Comment extends Model {
    use ServiceInjectionTrait;
    
    public $inject = [
        'storage' => Illuminate\Contracts\Filesystem\Filesystem::class
    ];
}

Use the array key, to access the service anywhere.

class Comment extends Model {
    use ServiceInjectionTrait;

    public $inject = [
        'storage' => Illuminate\Contracts\Filesystem\Filesystem::class
    ];
    
    public function storeAttachment($path, $contents)
    {
        $this->storage->put($path, $contents);
    }
}

Other usages

Wheres the trait has been designed for Eloquent models, it can be used in pretty much any class. Add and use it the same way as with models.

Contributing

First of all: Thank you for considering to contribute to this project. Without you the open source community would not be the same. If you decide to submit a pull request to this project, I'd kindly ask you to adhere to the following guidelines:

  • Be aware of the scope of the project, to avoid disappointment.
  • Please add appropriate tests to all pull requests.
  • When interacting on this project, follow the Code of Conduct.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-08