Custom activity models | laravel-activitylog-rust | Spatie       

 SPATIE  

laravel-activitylog-rust
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog-rust](https://spatie.be/docs/laravel-activitylog-rust/v1)  Advanced-usage  Custom activity models

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-activitylog-rust/v1) 

  Custom activity models    
- [ Introduction ](https://spatie.be/docs/laravel-activitylog-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-activitylog-rust/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog-rust/v1/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-activitylog-rust/v1/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog-rust/v1/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog-rust/v1/changelog)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog-rust/v1/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog-rust/v1/about-us)
- [ Testing ](https://spatie.be/docs/laravel-activitylog-rust/v1/testing)

Basic usage
-----------

- [ Logging activity ](https://spatie.be/docs/laravel-activitylog-rust/v1/basic-usage/logging-activity)
- [ Cleaning up the log ](https://spatie.be/docs/laravel-activitylog-rust/v1/basic-usage/cleaning-up-the-log)

Advanced usage
--------------

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/logging-model-events)
- [ Log options ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/log-options)
- [ Buffering activities ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/buffering)
- [ Default causer for a logger ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/disabling-logging)
- [ Custom activity models ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/customizing-actions)
- [ Before logging hook ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/before-logging-hook)
- [ Causer identity ](https://spatie.be/docs/laravel-activitylog-rust/v1/advanced-usage/causer-resolver)

 Custom activity models
======================

Select an application model with `service_provider_with_model::<Audit>()`. The model uses the native `#[model]` macro and implements `ActivityRecord`. Resolve `ModelActivityLog<Audit>`from the container and register `observe_with_model::<Article, Audit>(events)` for each subject. The short `ActivityLog` name selects the package's standard `Activity` model.

- `ActivityRecord::insert_activity(config, draft)` inserts using your model's native `create!` or typed create builder and returns its integer ID. Map the draft fields to your columns, add tenant values, and let native casts and model events run.
- Override `ActivityRecord::clean_activities(config, cutoff, log)` for a custom retention policy. Both `logger.clean` and `activitylog:clean` call it. The default deletes rows strictly older than the cutoff on the configured table and connection.
- The provider takes table and connection defaults from `Audit::META`. Application `Config` overrides still run last. If your insertion uses a fixed model destination, keep those settings aligned with its metadata, or select the connection explicitly in your insertion. Queries, buffering transactions and cleanup use the final config.
- `logger.query(...)` returns your actual model, including custom columns. Its selected columns come from native metadata, not the package's standard activity projection.
- Supply an application migration for the custom table. The bundled migration still creates `activity_log`; it does not infer or create your model's schema.
- `ModelActivityLog::<Audit>::for_model(config)` constructs an explicitly owned logger using model table/connection defaults. Enable its buffer and call `flush` explicitly.
- Keep an integer `id`, `created_at` for cleanup, and the standard columns used by any `ActivityQuery` filters you call. An `updated_at` column is optional in your own model.

The integration test in `tests/custom_model.rs` is a complete executable example. It uses a tenant activity model on a separate `audit` connection, writes an extra tenant column, captures its native `Created<TenantActivity>` events with `event::fake`, and verifies observers, typed reads and buffered flush use that destination. Its cleanup policy protects other tenants' records through both the direct API and console command.

Use [hooks](before-logging-hook) for enrichment or redaction before persistence. `draft.extra` supports application columns on the standard model's destination. A custom model decides how to map extra values. Cross-database writes are not atomic.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
