laravel-activity-presenter maintained by deifhelt
Description
Activity presenter for Laravel.
Author
Last update
2026/04/23 06:54
(dev-dependabot/github_actions/dependabot/fetch-metadata-3.1.0)
License
Downloads
24
Tags
Laravel Activity Presenter
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.
- Object-Oriented: Provides rich objects (
LogEntry,AttributeChange) instead of flat strings, giving you full control in your View. - 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. - Agnostic & Flexible: Does not force date formats or string styles. You get the raw
Carbonobjects and Models to format however you like.
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 $log)
<div class="activity-item">
<!-- Full Control over Date Format -->
<span class="date">{{ $log->activity->created_at->diffForHumans() }}</span>
<!-- "John Doe created Project Alpha" -->
<p>
@if($log->causer)
<a href="{{ route('users.show', $log->causer) }}">
<strong>{{ $log->getCauserLabel() }}</strong>
</a>
@else
System
@endif
{{ $log->getEventLabel() }}
<strong>{{ $log->getSubjectLabel() }}</strong>
</p>
<!-- Show changes: "Status: Pending -> Active" -->
<ul>
@foreach($log->changes as $change)
<li>
{{ $change->key }}:
<span class="text-red-500">{{ $change->old }}</span>
→
<!-- If it's a resolved model (e.g. status_id -> Status Model), link to it! -->
@if($change->relatedModel)
<a href="{{ route('statuses.show', $change->relatedModel) }}" class="text-green-500">
{{ $change->relatedModel->name }}
</a>
@else
<span class="text-green-500">{{ $change->new }}</span>
@endif
</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.