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
LogsActivity
和
CausesActivity
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_name
和
database_connection
config options are also gone.
Version 5 requires PHP 8.4+ and Laravel 12+. The full 升级指南 和 变更日志 are available on GitHub 。





