定制 kenarkose/ownable 二次开发

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

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

kenarkose/ownable

最新稳定版本:1.4.0

Composer 安装命令:

composer require kenarkose/ownable

包简介

Easy ownership for Eloquent Models on Laravel 5

README 文档

README

Easy ownership for Eloquent Models with Laravel 5.

Build Status Total Downloads Latest Stable Version License

Ownable provides a simple solution for associating owners (most likely User models) to individual models.

Features

  • Compatible with Laravel 5
  • Configurable
  • Auto associating the authorized user by Laravel on creation as owner
  • Changing owners
  • Eloquent relationships
  • A phpunit test suite for easy development

Installation

Installing Ownable is simple. Just pull the package in through Composer.

{
    "require": {
        "kenarkose/ownable": "~1.0"
    }
}

Usage

Just add the traits to the model which you want to become ownable and customize if neccessary.

use Illuminate\Database\Eloquent\Model;
use Kenarkose\Ownable\AutoAssociatesOwner;
use Kenarkose\Ownable\Ownable;
use Kenarkose\Ownable\Contract\Ownable as OwnableContract;

class OwnableItem extends Model implements OwnableContract {
    
    use Ownable, AutoAssociatesOwner;
    
}

$ownable = OwnableItem::find();

// You may access owner like so:
$owner = $ownable->getOwner();

// or, you may use the Eloquent relation and dynamic property:
$owner = $ownable->owner;
$ownerRelation = $ownable->owner();


$ownable = new OwnableItem;

// You may change and remove owner and persist like so:
$ownable->changeOwnerTo($owner);
$ownable->abandonOwner();

// or, if you would like to do these without saving the model you can use:
$ownable->associateOwner($owner);
$ownable->dissociateOwner();

// You may also verify and check owners
$ownable->hasOwner();
$ownable->isOwnedBy($owner);

The AutoAssociatesOwner trait is optional and should be used if auto association of logged in user on model creation is desired. In other words, if the trait is used, the model will associate the logged in user as the owner.

Customization

You may customize individual models that use the Ownable and AutoAssocicatesOwner traits by adding relevant attributes. The default configuration are as listed below.

protected $ownerModel = 'App\User';
protected $ownerKey = 'user_id';
protected $ownerParentKey = 'id';

All of these attributes are optional and they fallback to defaults if not set.

Finally, if you would like to change the default auto association behaviour, you may do so by overriding the associateDefaultOwner method.

use Illuminate\Database\Eloquent\Model;
use Kenarkose\Ownable\Ownable;
use Kenarkose\Ownable\AutoAssociatesOwner;
use Kenarkose\Ownable\Contract\Ownable as OwnableContract;

class OwnableItem extends Model implements OwnableContract {
    
    use Ownable, AutoAssociatesOwner;
    
    public function associateDefaultOwner()
    {
        // Some other logic
    }
}

License

Ownable is released under MIT License.

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固