Jump24——Laravel 开发人员,精准到位。绝不外包。绝不离岸外包。始终卓越。

Log User Activity in Your Laravel App with Activity Log v5

最后更新于 经过

Log User Activity in Your Laravel App with Activity Log v5  image

Laravel Activitylog by Spatie is a package for recording what users do in your Laravel application. It stores all activity in an activity_log database table and supports both manual logging and automatic event tracking on Eloquent models.

Recently released, version 5 ships with PHP 8.4 and Laravel 12 as minimum requirements, removes some legacy features, and modernizes the API.

HasActivity Trait

新的 HasActivity trait combines the previously separate LogsActivityCausesActivity traits into one. Adding this trait to your model is all you need to start tracking created, updated, and deleted events automatically:

使用 Spatie\Activitylog\Models\Concerns\HasActivity ;
班级 用户 延伸 模型
{
使用 HasActivity ;
}

HasActivity , the model can both trigger logged events (as a subject) and be associated as the causer of activity on other models. The individual traits remain available if you need them separately.

Activity Buffering

By default, every logged activity fires an immediate INSERT query. If your application logs many activities in a single request — for example, during batch model updates — this can add up quickly. The new buffering feature collects activities in memory and flushes them in a single bulk query after the response is sent.

Enable it in your .env :

ACTIVITYLOG_BUFFER_ENABLED = 真的

Or directly in config/activitylog.php :

'buffer' => [
‘已启用’ => 真的 ,
],

No other code changes are required. Both automatic model event logging and manual activity()->log() calls are buffered automatically.

A few things to keep in mind:

  • No ID until flush — buffered activities won't have a database ID until the buffer is flushed, so don't enable this if you need the ID immediately after logging.
  • Octane compatible — the buffer is a scoped binding, so it resets between requests automatically.
  • Queue compatible — the buffer is flushed after each job completes (or fails), so activities logged inside a job are bulk inserted when the job finishes.

Default Causer

Activity::defaultCauser() sets a default causer for logged activities. Pass just a model to set a persistent default, or pass a model and a closure to scope the causer to that callback:

活动 :: defaultCauser ($adminUser, 功能 () 使用 ($post) {
activity () -> performedOn ($post) -> 日志 “已发布” (英文):
activity () -> performedOn ($post) -> 日志 'notified subscribers' (英文):
});

attribute_changes Column

Model change tracking now uses a dedicated attribute_changes database column instead of storing changes inside the general-purpose properties JSON column. The $activity->changes() method has been replaced by direct property access:

// v4
活动 -> 变化 ();
// v5
活动 -> attribute_changes;

properties column still exists for custom data set with withProperties() 或者 withProperty()

重大变化

Several method and config names changed for consistency. Notable renames:

Old 新的
activities() activitiesAsSubject()
actions() activitiesAsCauser()
dontSubmitEmptyLogs() dontLogEmptyChanges()
withoutLogs() withoutLogging()
getExtraProperty() getProperty()
ACTIVITY_LOGGER_ENABLED 环境 ACTIVITYLOG_ENABLED
delete_records_older_than_days 配置 clean_after_days

The batch and pipe systems have been removed entirely, including LogBatch , Activity::batch() , LoggablePipe , 和 EventLogBag 。这 table_namedatabase_connection config options are also gone.

Version 5 requires PHP 8.4+ and Laravel 12+. The full 升级指南变更日志 are available on GitHub

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 代码审查

几天内即可获得 Laravel 代码审查方面的专家指导

访问 Laravel 代码审查
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
JSON Alexander Gives Developers a Simpler, More Trustworthy Way to View JSON in the Browser image

JSON Alexander Gives Developers a Simpler, More Trustworthy Way to View JSON in the Browser

阅读文章
Manage Software Licenses in Laravel with Laravel Licensing image

Manage Software Licenses in Laravel with Laravel Licensing

阅读文章
Matt Stauffer Joins the PHP Foundation Board — What It Means for Laravel image

Matt Stauffer Joins the PHP Foundation Board — What It Means for Laravel

阅读文章
Log User Activity in Your Laravel App with Activity Log v5  image

Log User Activity in Your Laravel App with Activity Log v5

阅读文章
使用新的 Cloud CLI 镜像从终端管理 Laravel Cloud

使用新的 Cloud CLI 从终端管理 Laravel Cloud

阅读文章
Laravel 13.3.0 图片中的单元测试属性及其他功能

Laravel 13.3.0 中的单元测试属性及更多功能

阅读文章