定制 jhonoryza/laravel-model-history 二次开发

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

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

jhonoryza/laravel-model-history

Composer 安装命令:

composer require jhonoryza/laravel-model-history

包简介

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

This package provides ability to generate _logs table from your model.

Requirement

  • PHP 8.1 - 8.4
  • Laravel 9, 10, 11, 12

Getting Started

install

composer require jhonoryza/laravel-model-history

Usage

let's say you want to create table category_logs

we can achieve this with running this command:

php artisan model-history:generate categories

it will generate new migration for table category_logs and new model CategoryLog

then you can add to the log like this :

<?php

use Jhonoryza\LaravelModelHistory\Factory\History;
use App\Models\CategoryLog;

History::make(CategoryLog::class)
    ->model($category)                      // required
    ->changeBy(auth()->user())              // optional
    ->old($oldData)                         // optional
    ->new($newData)                         // optional
    ->operation(History::UPDATE)            // required
    ->log('Category updated successfully'); // required

to get all the logs

<?php

return History::make(CategoryLog::class)
    ->getLogs($category->id);

Detect From Model Event

you can make adding log automatically by detecting from model event

like created, updated, deleted, restored and force deleted

by adding trait ModelHistoryTrait to your model, then it will create log entry automatically

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Jhonoryza\LaravelModelHistory\Traits\ModelHistoryTrait;

class Category extends Model
{
    use ModelHistoryTrait;

    protected static function historyModel(): string
    {
        return CategoryLog::class;
    }
}

Laravel Eloquent Events Reference

Available events :

  • retrieved
  • creating / created
  • updating / updated
  • saving / saved
  • deleting / deleted
  • restoring / restored
  • forceDeleted

Create & Update Operations

Function Fires Events? Events Triggered Notes
Model::create([...]) ✅ Yes creating, created, saving, saved Uses model instance
Model::query()->create([...]) ✅ Yes Same as above Proxy to create()
new Model([...])->save() ✅ Yes Same as create() Instance save
$model->save() (new) ✅ Yes creating, created, saving, saved Insert
$model->save() (existing + dirty) ✅ Yes updating, updated, saving, saved Update
$model->save() (existing + not dirty) ❌ No No SQL → no events
$model->saveQuietly() ❌ No Saves silently
$model->update([...]) ✅ Yes updating, updated, saving, saved Instance update
Model::query()->update([...]) ❌ No Bulk update, direct SQL
DB::table('table')->update([...]) ❌ No Query Builder
$model->updateQuietly([...]) ❌ No Silent update
DB::table('table')->insert([...]) ❌ No Pure Query Builder
DB::table('table')->insertGetId([...]) ❌ No Pure Query Builder
Model::insert([...]) (mass insert array of rows) ❌ No Static insert, bypasses model

Delete & Soft Delete

Function Fires Events? Events Triggered Notes
$model->delete() ✅ Yes deleting, deleted SoftDeletes → soft delete
$model->forceDelete() ✅ Yes deleting, forceDeleted Permanent
Model::destroy([1,2]) ✅ Yes Fires per model instance
Model::query()->delete() ❌ No Bulk delete
Model::query()->forceDelete() ❌ No Bulk permanent delete
DB::table('table')->delete() ❌ No Query Builder
DB::table('table')->truncate() ❌ No Direct truncate, no events
Model::truncate() ❌ No Shortcut ke DB::table()->truncate()

Restore (SoftDeletes only)

Function Fires Events? Events Triggered Notes
$model->restore() ✅ Yes restoring, restored Restores model
Model::query()->restore() ❌ No Bulk restore, direct SQL

Retrieval

Function Fires Events? Events Triggered Notes
Model::find(1) ✅ Yes retrieved Fires after hydrating model
Model::all() ✅ Yes retrieved (per model) For each row
Model::query()->get() ✅ Yes retrieved (per model) Same as above
DB::table('table')->get() ❌ No Query Builder only

Other Methods

Function Fires Events? Events Triggered Notes
$model->push() ⚠️ Partial Parent: same as save(); Relations: saved directly, no events
$model->touch() ✅ Yes updating, updated, saving, saved Updates timestamps
$model->refresh() ❌ No Reload from DB
$model->replicate() ❌ No Just copies attributes, no save
$model->firstOrCreate() ✅ Yes Either retrieved OR (creating, created, saving, saved)
$model->updateOrCreate() ✅ Yes Either (updating, updated) OR (creating, created)

React Component

there is prebuilt react component in this path resources/js/components/log-panel-history.tsx.

Usage

example use in controller

<?php

    public function show(Game $game): Response
    {
        return Inertia::render('builder/show', [
            'form' => GameForm::view($game),
            'logs' => History::make(GameLog::class)
                ->getLogs($game->id),
        ]);
    }

example use in edit.tsx

{
  logs && logs.length > 0 && <LogHistoryPanel logs={logs} />;
}

Security

If you've found a bug regarding security, please mail jardik.oryza@gmail.com instead of using the issue tracker.

License

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

jhonoryza/laravel-model-history 适用场景与选型建议

jhonoryza/laravel-model-history 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jhonoryza/laravel-model-history 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-10