satya / eloquent-activity

这将管理 eloquent 活动,并将其保存到数据库

v1.0.0 2020-12-31 19:43 UTC

This package is auto-updated.

Last update: 2024-09-29 06:25:44 UTC


README

Laravel Eloquent Activity Manage Package

Laravel Eloquent Activity Manage Package

这将管理 eloquent 活动,并将其保存到数据库,开发者可以轻松管理 Laravel 应用程序的活动。

安装

使用 composer 安装包

composer require satya/eloquent-activity

在 config/app.php 文件中注册服务提供者

'providers' => [
    // ...
    Satya\EloquentActivity\EloquentActivityServiceProvider::class,
];

运行迁移

php artisan migrate

基本用法

首先,将 Satya\EloquentActivity\Traits\EloquentActivity 特性添加到您的模型(s)中

use Illuminate\Database\Eloquent\Model;
use Satya\EloquentActivity\Traits\EloquentActivity;

class VendorCenter extends Model
{
    use EloquentActivity;

    //..
}

自定义 Eloquent 标签名称设置

默认情况下,该包使用日志名称作为 eloquent 名称。例如,VendorCenter 是类名,因此日志标签名称考虑为“Vendor Center”。如果您想自定义标签名称,只需在模型类体中声明受保护的变量 tagName。

protected $tagName = 'Vendor Information';
use Illuminate\Database\Eloquent\Model;
use Satya\EloquentActivity\Traits\EloquentActivity;

class VendorCenter extends Model
{
    use EloquentActivity;
    
    protected $tagName = 'Vendor Information';

    //..
}

检索应用程序活动

要检索应用程序活动,请使用 Satya\EloquentActivity\Model\EloquentActivity 模型。

use Satya\EloquentActivity\Model\EloquentActivity;

class VendorCenterController extends Controller
{
    
    public function index(){
        //Retrieving app activity
        $activity = EloquentActivity::orderBy('created_at','DESC')->get();
    }
}

检索特定记录历史的示例

此包具有通过调用记录历史 morphMany 关系方法查看特定记录历史的功能。

  • 控制器
     /**
         * @param $id
         */
        public function history($id){
            $client = Client::where('id',$id)->first();
            if (!empty($client))
            {
                dump($client);
                dd($client->recordHistory->toArray());
            }
        }
  • 模型
    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use Satya\EloquentActivity\Traits\EloquentActivity;
    
    class Client extends Model
    {
        use EloquentActivity;
    
        /**
         * @var string
         */
        protected $table = 'clients';
    
    
        /**
         * @var string[]
         */
        protected $fillable = [
            'first_name',
            'last_name'
        ];
    }
  • 结果

如何在没有任何包的情况下实现 Eloquent Activity 功能。

以下文章链接对在 Laravel 应用程序上实现此包功能而无需任何包很有帮助。

链接:- https://satyaprakash-nishad.medium.com/laravel-model-custom-logs-with-traits-89a246d8bf1c