Package Data | |
---|---|
Maintainer Username: | RajuEz |
Maintainer Contact: | freek@spatie.be (Freek Van der Herten) |
Package Create Date: | 2015-04-16 |
Package Last Update: | 2015-05-27 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-17 03:03:01 |
Package Statistics | |
---|---|
Total Downloads: | 154 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This Laravel 5 package provides a very easy to use solution to log the activities of the users of your Laravel 5 app. All the activities will be logged in a db-table. Optionally the activities can also be logged against the default Laravel Log Handler.
Created by Freek Van der Herten (@freekmurze)
If you're using Laravel 4, take a look at version 0.3.0 of this package.
This package can be installed through Composer.
composer require spatie/activitylog
This service provider must be registered.
// config/app.php
'providers' => [
'...',
'Spatie\Activitylog\ActivitylogServiceProvider',
];
You'll also need to publish and run the migration in order to create the db-table.
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="migrations"
php artisan migrate
Activitylog also comes with a facade, which provides an easy way to call it.
// config/app.php
'aliases' => [
...
'Activity' => 'Spatie\Activitylog\ActivitylogFacade',
];
Optionally you can publish the config file of this package.
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
The configuration will be written to config/activitylog.php
. The options provided are self explanatory.
Logging some activity is very simple.
/*
The log-function takes two parameters:
- $text: the activity you wish to log.
- $user: optional can be an user id or a user object.
if not proved the id of Auth::user() will be used
*/
Activity::log('Some activity that you wish to log');
The string you pass to function gets written in a db-table together with a timestamp, the ip address and the user agent of the user.
This package can log the events from your models. To do so your model must use the LogsActivity
-trait and implement LogsActivityInterface
.
use Spatie\Activitylog\LogsActivityInterface;
use Spatie\Activitylog\LogsActivity;
class Article implements LogsActivityInterface {
use LogsActivity;
...
The interface expects you to implement the getActivityDescriptionForEvent
-function.
Here's an example of a possible implementation.
/**
* Get the message that needs to be logged for the given event name.
*
* @param string $eventName
* @return string
*/
public function getActivityDescriptionForEvent($eventName)
{
if ($eventName == 'created')
{
return 'Article "' . $this->name . '" was created';
}
if ($eventName == 'updated')
{
return 'Article "' . $this->name . '" was deleted';
}
if ($eventName == 'deleted')
{
return 'Article "' . $this->name . '" was deleted';
}
return '';
}
The result of this function will be logged, unless the result is an empty string.
All events will be logged in the activity_log
-table. This package provides an Eloquent model to work with the table. You can use all the normal Eloquent methods that you know and love. Here's how you can get the last 100 activities together with the associated users.
use Spatie\Activitylog\Models\Activity;
$latestActivities = Activity::with('user')->latest()->limit(100)->get();
Over time your log will grow. To clean up the database table you can run this command:
Activity::cleanLog();
By default records older than 2 months will be deleted. The number of months can be modified in the config-file of the package.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.