承接 elaborate-code/laravel-eloquent-logs 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

elaborate-code/laravel-eloquent-logs

最新稳定版本:1.1.0

Composer 安装命令:

composer require elaborate-code/laravel-eloquent-logs

包简介

A simple way to log changes that occur on Eloquent models

README 文档

README

Packagist Version Packagist Downloads (custom server) GitHub Workflow Status GitHub Workflow Status maintained Production ready

banner

Log what happens to your Eloquent models (created|updated|deleted|soft deleted|restored|force deleted) and keep and eye on who made the change, how and when.

This solution is simple to integrate and introduces minimal changes to your project: 1 migration, 1 model, 1 trait, and 1 facade.

Installation

Install the package via composer:

composer require elaborate-code/laravel-eloquent-logs

Publish the migrations:

php artisan vendor:publish --tag="eloquent-logs-migrations"

Run the migrations:

php artisan migrate

Publishing config file [Optional]

You can publish the config file with:

php artisan vendor:publish --tag="eloquent-logs-config"

This is the contents of the published config file:

return [
    'logs_model' => \ElaborateCode\EloquentLogs\Models\EloquentLog::class,
    'logs_table' => 'eloquent_logs',

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

That allows you to rename the logs_table before running the migrations.

Usage

Pick an Eloquent model that you want to log the changes that happen to it and add the HasLogs trait to it.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    use \ElaborateCode\EloquentLogs\Concerns\HasLogs;
    // ...
}

After adding that trait, every change made to the model will be recorded.

Important warning from Laravel docs

When issuing a mass update or delete query via Eloquent, the saved, updated, deleting, and deleted model events will not be dispatched for the affected models. This is because the models are never actually retrieved when performing mass updates or deletes.

Retrieving logs

You can load a model's logs using the eloquentLogs relationship:

$example_model->eloquentLogs;

$example_model->load('eloquentLogs');

App\Models\ExampleModel::with('eloquentLogs')->find($id);

And you can query logs directly:

// latest 5 logs with affected models
ElaborateCode\EloquentLogs\Models\EloquentLog::with('loggable')->latest()->limit(5)->get()

Grouping queries

By default each one model event will result in a query to log the action.

$example_model = ExampleModel::create(['name' => 'foo']);

$example_model->update(['name' => 'bar']);

$example_model->delete();

// ⚠️ This will result in 3 queries to insert the 3 events logs into the database  

You can improve the logging process by using the CacheEloquentLogQueries facade

use ElaborateCode\EloquentLogs\Facades\CacheEloquentLogQueries;

CacheEloquentLogQueries::start();

$example_model = ExampleModel::create(['name' => 'foo']);

$example_model->update(['name' => 'bar']);

$example_model->delete();

CacheEloquentLogQueries::execute();

// 👍 This will result in 1 query to insert the 3 events logs into the database  

The facade includes other methods that you wouldn't necessarily need to use:

// Stops caching and empties the cache without queries execution
CacheEloquentLogQueries::reset();

// Empties the cache but doesn't stop caching
CacheEloquentLogQueries::flushQueries();

// Stops caching until the reuse of start() and doesn't empty the cache
CacheEloquentLogQueries::suspend();

// Returns a boolean
CacheEloquentLogQueries::isCaching();

Ignoring events

You can specify the events to not log on the model instances by listing the events to ignore on YourModel::$loggableOptions['ignore'].

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    use \ElaborateCode\EloquentLogs\Concerns\HasLogs;

    public static array $loggableOptions = [
        'ignore' => ['created', 'updated', 'deleted', 'softDeleted', 'forceDeleted', 'restored'],
    ];
    // ...
}

Muting Eloquent events [Laravel stuff]

From seeders:

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents; // Add this trait

    public function run(): void
    {
        // Silent eloquent queries ...
    }
}

Anywhere from your code:

\Illuminate\Database\Eloquent\Model::unsetEventDispatcher();

// Silent eloquent queries ...

\Illuminate\Database\Eloquent\Model::setEventDispatcher(app(Dispatcher::class));
// ...

Explore the Eloquent docs for more options

Alternative

Among the bajillion packages that Spatie has so graciously bestowed upon the community, you'll find the excellent laravel-Alternative package.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

elaborate-code/laravel-eloquent-logs 适用场景与选型建议

elaborate-code/laravel-eloquent-logs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 278 次下载、GitHub Stars 达 19, 最近一次更新时间为 2022 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 elaborate-code/laravel-eloquent-logs 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-23