deifhelt/laravel-activity-presenter
最新稳定版本:v1.0.0
Composer 安装命令:
composer require deifhelt/laravel-activity-presenter
包简介
Activity presenter for Laravel.
README 文档
README
Laravel Activity Presenter is a powerful presentation layer for spatie/laravel-activitylog. It solves the common challenges of displaying activity logs in your application: resolving relationships efficiently, formatting data consistently, and handling translations.
Why use this?
When displaying activity logs, you often face these issues:
- N+1 Queries: Showing "User X updated Project Y" requires loading the User and Project models for every log entry.
- Missing Context: If "Project Y" is deleted, you still want to show its name in the log history, but the relationship is null.
- Unformatted Data: You have
user_id: 5in the log properties, but you want to display "John Doe". - Inconsistent Presentation: You find yourself repeating
trans('...')...logic in every Blade view.
This package solves all of them.
Key Features
- 🚀 Smart Resolution: Automatically resolves related models (User, Subject) in a single optimized query to prevent N+1 issues.
- 💎 Unified DTO: Transforms raw log data into a consistent
ActivityPresentationDTOclass for easy use in Views and APIs. - 🔌 Config-Driven: Define how specific attributes (like
category_id) map to models in a simple config file. - 🌍 Auto-Localization: Built-in support for translating events (
created->Creado), model names (User->Usuario), and attributes. - 🛡️ Graceful Fallbacks: If a related model is permanently deleted, it gracefully falls back to historical data (e.g., "Project #123").
Installation
Install via Composer (Spatie Activitylog is included automatically):
composer require deifhelt/laravel-activity-presenter
Publish the configuration:
php artisan vendor:publish --provider="Deifhelt\ActivityPresenter\ActivityPresenterServiceProvider"
Quick Usage
1. In your Controller
use Deifhelt\ActivityPresenter\Facades\ActivityPresenter; use Spatie\Activitylog\Models\Activity; public function index() { // Fetch logs (paginated) $activities = Activity::latest()->paginate(20); // Present them (loads relations, formats dates, translates events) $presented = ActivityPresenter::presentCollection($activities); return view('activities.index', [ 'activities' => $presented ]); }
2. In your Blade View
@foreach($activities as $activity) <div class="activity-item"> <span class="date">{{ $activity->diff }}</span> <!-- "John Doe created Project Alpha" --> <p> <strong>{{ $activity->user_name }}</strong> {{ $activity->event }} <strong>{{ $activity->subject_name }}</strong> </p> <!-- Show changes: "Status: Pending -> Active" --> <ul> @foreach($activity->new_values as $key => $value) <li> {{ $key }}: <span class="text-red-500">{{ $activity->old_values[$key] ?? 'N/A' }}</span> → <span class="text-green-500">{{ $value }}</span> </li> @endforeach </ul> </div> @endforeach
Documentation
- Installation & Configuration - Deep dive into setup and config options.
- Logging Guide - Best practices for logging model events.
- Usage Patterns - Advanced usage in Controllers, Views, and APIs.
- Localization - How to translate every aspect of your logs.
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-01