2017 © Pedro Peláez
 

library eloquent-service-injection

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

image

mrgrain/eloquent-service-injection

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

  • Monday, February 13, 2017
  • by mrgrain
  • Repository
  • 1 Watchers
  • 0 Stars
  • 759 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Eloquent Service Injection

Simple trait to inject services into Eloquent models via a property., (*1)

Build Status Latest Stable Version Total Downloads License, (*2)

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)., (*3)

Requirements

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

Installation

Add the package to your repository with Composer., (*4)

composer require mrgrain/eloquent-service-injection

Usage

Extend your models by using the trait:, (*5)

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., (*6)

Add the services to be injected into the $inject property., (*7)

class Comment extends Model {
    use ServiceInjectionTrait;

    public $inject = [
        'storage' => Illuminate\Contracts\Filesystem\Filesystem::class
    ];
}

Use the array key, to access the service anywhere., (*8)

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., (*9)

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:, (*10)

  • 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.

The Versions

13/02 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Moritz Kornher

13/02 2017

1.1.0

1.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Moritz Kornher

08/11 2015

1.0.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Moritz Kornher