定制 richan-fongdasen/eloquent-blameable 二次开发

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

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

richan-fongdasen/eloquent-blameable

Composer 安装命令:

composer require richan-fongdasen/eloquent-blameable

包简介

Blameable behavior implementation for your Eloquent Model in Laravel

README 文档

README

CI codecov Total Downloads Latest Stable Version License: MIT

Eloquent Blameable

Blameable behavior implementation for your Eloquent Model in Laravel

Synopsis

This package would help you to track the creator and updater of each database record. It would be done by filling the specified attributes with the current user ID automatically. By default, those attributes would be filled when you are saving the Eloquent Model object.

Table of contents

Setup

Install the package via Composer :

$ composer require richan-fongdasen/eloquent-blameable

Laravel version compatibility

Laravel version Blameable version PHP version
5.1.x 1.0.x >= 5.6
5.2.x - 5.4.x 1.1.x - 1.2.x >= 5.6
5.5.x - 5.8.x 1.3.x >= 7.0
6.x 1.4.x >= 7.2
7.x 1.5.x >= 7.2
8.x 1.6.x >= 7.3
9.x 1.8.x >= 8.0
10.x 1.9.x - 1.10.x >= 8.1
11.x - 12.x 1.11.x >= 8.0
13.x 2.0.x >= 8.3

If you are using Laravel version 5.5+ then you can skip registering the service provider in your Laravel application.

Service Provider

Add the package service provider in your config/app.php

'providers' => [
    // ...
    RichanFongdasen\EloquentBlameable\ServiceProvider::class,
];

Configuration

Publish configuration file using php artisan command

$ php artisan vendor:publish --provider="RichanFongdasen\EloquentBlameable\ServiceProvider"

The command above would copy a new configuration file to /config/blameable.php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Guard
    |--------------------------------------------------------------------------
    |
    | Please specify your default authentication guard to be used by blameable
    | service. You can leave this to null if you're using the default Laravel
    | authentication guard.
    |
    | You can also override this value in model classes to use a different
    | authentication guard for your specific models.
    | IE: Some of your models can only be created / updated by specific users
    | who logged in from a specific authentication guard.
    |
    */

    'guard' => null,

    /*
    |--------------------------------------------------------------------------
    | User Model Definition
    |--------------------------------------------------------------------------
    |
    | Please specify a user model that should be used to setup `creator`
    | and `updater` relationship.
    |
    */

    'user' => \App\Models\User::class,

    /*
    |--------------------------------------------------------------------------
    | The `createdBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the creator
    | identifier.
    |
    */

    'createdBy' => 'created_by',

    /*
    |--------------------------------------------------------------------------
    | The `updatedBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the updater
    | identifier.
    |
    */

    'updatedBy' => 'updated_by',

    /*
    |--------------------------------------------------------------------------
    | The `deletedBy` attribute
    |--------------------------------------------------------------------------
    |
    | Please define an attribute to use when recording the user
    | identifier who deleted the record. This feature would only work
    | if you are using SoftDeletes in your model.
    |
    */

    'deletedBy' => 'deleted_by',
];

Usage

Add some blameable attributes to your migrations

Schema::create('some_tables', function (Blueprint $table) {
    // ...

    $table->integer('created_by')->nullable();
    $table->integer('updated_by')->nullable();
    $table->integer('deleted_by')->nullable();

    // ...



    /**
     * You can also create foreign key constrains
     * for the blameable attributes.
     */
    $table->foreign('created_by')
        ->references('id')->on('users')
        ->onDelete('cascade');

    $table->foreign('updated_by')
        ->references('id')->on('users')
        ->onDelete('cascade');
});

Attach Blameable behavior into your Model

use Illuminate\Database\Eloquent\Model;
use RichanFongdasen\EloquentBlameable\BlameableTrait;

class Post extends Model
{
    use BlameableTrait;

    // ...
}

Override default configuration using static property

    /**
     * You can override the default configuration
     * by defining this static property in your Model
     */
    protected static $blameable = [
        'guard' => 'customGuard',
        'user' => \App\Models\User::class,
        'createdBy' => 'user_id',
        'updatedBy' => null
    ];

Override default configuration using public method

    /**
     * You can override the default configuration
     * by defining this method in your Model
     */
    public function blameable()
    {
        return [
            'guard' => 'customGuard',
            'user' => \App\Models\User::class,
            'createdBy' => 'user_id',
            'updatedBy' => null
        ];
    }

Using Blameable Query Scopes

// Get all posts which have created by the given user id
Post::createdBy($userId)->get();

// Get all posts which have updated by the given user object
$user = User::findOrFail(1);
Post::updatedBy($user)->get();

Accessing Creator / Updater Object

// Get the creator user object
Post::findOrFail($postId)->creator;

// Get the updater user object
Post::findOrFail($postId)->updater;

License

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

richan-fongdasen/eloquent-blameable 适用场景与选型建议

richan-fongdasen/eloquent-blameable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 225.3k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2017 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Blameable」 「laravel」 「eloquent」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 richan-fongdasen/eloquent-blameable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 richan-fongdasen/eloquent-blameable 我们能提供哪些服务?
定制开发 / 二次开发

基于 richan-fongdasen/eloquent-blameable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 225.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 30
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 34
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-06