Ever wanted to see WHO changed WHAT and WHEN inside your app? Want to remember all the DB changes your users have done? Well... this package doesn't do that. spatie/laravel-activitylog
does, and does it very well. This package adds a web interface for it, for projects using Backpack for Laravel. It gives your admin/superadmin the ability to see
- the activities performed by certain models;
- the activities performed on certain models;
- and more;
How does it all work? Well:
- when a change happens to an Eloquent model, the Spatie package will make a note of it in the database;
- this package adds a web interface, so the admin can see the changes (aka activity log);
NOTE: The filters are a Backpack\PRO feature. If you don't have that package the filters won't be available.
Try it right now, in our online demo. Edit some entities, and check the activity logs.
In your Laravel + Backpack project, install this package:
# install this interface package:
composer require backpack/activity-log
# add a menu item for it
php artisan backpack:add-menu-content "<x-backpack::menu-item title=\"Activity Logs\" icon=\"la la-stream\" :link=\"backpack_url('activity-log')\" />"
But also, if your package didn't already have spatie/laravel-activitylog
installed and set up, please follow the installation steps in their docs. We'll also copy-paste them here, for your convenience:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
php artisan migrate
Note: If your models are not configured to create activity logs yet, read the FAQ on how to log all model events. If you are unsure, then you have not configured them to log activities - read it.
Use it to browse all activity, filter the view, search the view, etc:
Use it to see details about a particular activity:
If you want your CrudControllers to show links to their activities, just use one or both of the provided Backpack operations:
- ModelActivityOperation: Displays a general button, allowing users to see a comprehensive list of all activities for that Model.
- EntryActivityOperation: Adds line buttons to each entry in the list, enabling users to view activity logs specific to individual entries.
Say you have a UserCrudController
. If you want a new button to show up next to the Add User
button, that will take you to all of the activities of all Users, then use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation
on your UserCrudController
.
<?php
namespace App\Http\Controllers\Admin;
...
class ArticleCrudController extends CrudController
{
...
+ use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation;
...
Say you have a UserCrudController
. If you want a new button to show up next each entry, that will take you to all of the activities of that entry, then use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation
on your UserCrudController
.
<?php
namespace App\Http\Controllers\Admin;
...
class ArticleCrudController extends CrudController
{
...
+ use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation;
...
ModelActivityOperation
and EntryActivityOperation
buttons will point to the Activity log page with the current model as the subject. If your model is a causer (the one who performed the action, most of the times User
model) you can set the operation options to causer.
class ArticleCrudController extends CrudController
{
...
public function setup()
{
...
+ CRUD::set('activity-log.options', ActivityLogEnum::CAUSER);
}
By default, the nothing gets logged. Please configure spatie/laravel-activitylog
to do the logging according to your needs. See the question below for the most common use case.
Note: this is NOT a feature that is provided by this package. It's provided by
spatie/laravel-activitylog
. But we try to help document the most common use case we have found, so it's easier for you to do it.
You want a new Activity registered, whenever a model is created
, updated
, deleted
etc? So that there's a record of WHO did WHAT and WHEN it happened? Here's how you can set up spatie/laravel-activitylog
to log all model events.
Step 1. Create a new model trait at App\Models\Traits\LogsActivity.php
with the following content:
<?php
namespace App\Models\Traits;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity as OriginalLogsActivity;
trait LogsActivity
{
use OriginalLogsActivity;
/**
* Spatie Log Options
* By default will log only the changes between fillables
*
* @return LogOptions
*/
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logAll()->logOnlyDirty();
}
}
Step 2. Use that trait on all Models where you want all events logged:
<?php
namespace App\Models;
+use App\Models\Traits\LogsActivity;
use Backpack\CRUD\app\Models\Traits\CrudTrait;
class Article extends Model
{
use CrudTrait;
+ use LogsActivity;
...
Notice that this trait extends the default Spatie\Activitylog\Traits\LogsActivity
and defines the getActivitylogOptions()
method providing some reasonable defaults. If you want to customize, see details here and options here.
To customize the logged information, you can override the getActivitylogOptions
method in your models.
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable()
->logOnlyDirty();
}
Yes, you can! The Spatie laravel-activitylog package offers a range of advanced customization options. For detailed insights into these options, check out the official Log Options documentation by Spatie, at https://spatie.be/docs/laravel-activitylog/v4/api/log-options
If you haven't customized your UserCrudController
yet, you can achieve this by binding the Backpack controller to your custom controller. In your App\Providers\AppServiceProvider.php
:
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(
\Backpack\PermissionManager\app\Http\Controllers\UserCrudController::class,
\App\Http\Controllers\Admin\UserCrudController::class
);
Then in your App\Http\Controllers\Admin\UserCrudController.php
you can do something like:
use Backpack\PermissionManager\app\Http\Controllers\UserCrudController as OriginalUserCrudController;
class UserCrudController extends OriginalUserCrudController
{
use \Backpack\ActivityLog\Http\Controllers\Operations\ModelActivityOperation;
use \Backpack\ActivityLog\Http\Controllers\Operations\EntryActivityOperation;
}
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
This project was released under MIT License, so you can install it on top of any Backpack & Laravel project. Please see the license file for more information.