PHPackages                             johannesschobel/laravel-userhistory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. johannesschobel/laravel-userhistory

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

johannesschobel/laravel-userhistory
===================================

Provide the possibility to log every operation a user did.

105653PHP

Since Jul 15Pushed 5y ago2 watchersCompare

[ Source](https://github.com/johannesschobel/laravel-userhistory)[ Packagist](https://packagist.org/packages/johannesschobel/laravel-userhistory)[ RSS](/packages/johannesschobel-laravel-userhistory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Laravel Userhistory
===================

[](#laravel-userhistory)

Laravel Userhistory is a package that lets you document the activities, resective users has performed on Models.

Install
-------

[](#install)

Via Composer

```
$ composer require johannesschobel/laravel-userhistory
```

Getting Started
---------------

[](#getting-started)

Install the package via composer (see above) or require it directly in your `composer.json` file using:

```
"require" : {
   ...,
   "johannesschobel/laravel-userhistory" : "dev-master",
   ...
},
```

Add the new Package to your providers within your `config/app.php` file:

```
'providers' => [
   ...
   JohannesSchobel\UserHistory\UserHistoryServiceProvider::class,
   ...
],
```

Then, use the publish command to add the required files to your project:

```
$ php artisan vendor:publish --provider="JohannesSchobel\UserHistory\UserHistoryServiceProvider"
```

Migrate the database using the following command:

```
$ php artisan migrate
```

and start customizing the `config/userhistory.php` file.

The config entry `userhistory.models.user`, thereby, reference your main model. If you rely on the Laravel framework, this is already configured properly. The config entry `userhistory.models.userhistory`, however, references the Userhistory model. If you use the model provided by this package, everything is fine as well.

The `userhistory.actions` references a class, providing your actions, you want to be able to track. The package already ships with a few default actions (e.g., `SHOW`, `STORE`, `DELETE` and so on). However, you can add your own specific actions by simply creating your own class file like so:

```
use JohannesSchobel\UserHistory\Enums\UserHistoryEnum;
class UserHistoryActions extends UserHistoryEnum {

   const CREATE = 100;
   const UPDATE = 101;
   const DELETE = 102;

   const PROFILE_CHANGED = 110;
   const PROFILE_PASSWORD_CHANGED = 111;

   // and so on..
}
```

Finally, add the `UserHistoryTrait`, which is defined in this package, to your `User` model by adding the following line:

```
use UserHistoryTrait;
```

Usage
-----

[](#usage)

To log a user's action, simply do something like this:

```
// assume, that $user is the current user
// e.g., $user = Auth::user();

// also assume, that $object is an object to be saved to the database
$object->name = "foo";
$object->description = "bar";
$result = $object->save();

if($result) {
   // create a log entry indicating that the user has updated a given record
   $history = $user->logAction($object, UserHistoryActions::UPDATE);
}

// continue with your business logic
```

To return all `Userhistory` objects for a given user, simply call

```
// assume, that $user is the current user
// e.g., $user = Auth::user();
$userhistories = $user->userhistories;
foreach($userhistories as $userhistory) {
	$obj = $userhistory->getEntity();
	// object is now null (if the entity does not exist)
	// or it is an object of the given model class!
}
```

### More Complex Example

[](#more-complex-example)

You can easily create some kind of `timeline`, which provides information about all actions a user has done. In this example, we will use the [League/Fractal](https://github.com/thephpleague/fractal) which provides `Transformers`. Furthermore, the Laravel framework is used.

First, we create a `UserHistoryTransformer`, which might look like this:

```
class UserHistoryTransformer extends TransformerAbstract
{
    public function transform(Userhistory $history)
    {
        $entity = $history->getEntity();

        return [
            'id' 	    => $history->id,
            'text'      => Lang::get('useractivities.' . strtolower($history->action), [], app()->getLocale()),
            'name'      => $entity->title,

            'uri'       => $entity->getSelfURI(),

            'created_at' => $history->created_at,
        ];
    }
}
```

Next, we will define respective endpoint, which will allow us to retrieve all required information. Lets create the endpoint `GET /my/activities` in Laravel's route file.

Finally, wire the respective controller method and the transformer together. This might look like this:

```
class MyController extends Controller {
    // more methods here

    public function myActivities(Request $request) {
        $user = // authenticate the current user from the request (e.g., by using JWT)

        $histories = $user->userhistories;

        return $this->response()->collection($histories, new UserHistoryTransformer());
    }
}
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Security
--------

[](#security)

If you discover any security related issues, please email :author\_email instead of using the issue tracker.

Credits
-------

[](#credits)

- Johannes Schobel \[\]

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/554f30f3c24b34c1961da6f12c0e2cfec6436e1c6dd6b8092669a7138086d770?d=identicon)[johannesschobel@googlemail.com](/maintainers/johannesschobel@googlemail.com)

---

Top Contributors

[![johannesschobel](https://avatars.githubusercontent.com/u/9431350?v=4)](https://github.com/johannesschobel "johannesschobel (2 commits)")[![rob-k](https://avatars.githubusercontent.com/u/3353474?v=4)](https://github.com/rob-k "rob-k (1 commits)")

### Embed Badge

![Health badge](/badges/johannesschobel-laravel-userhistory/health.svg)

```
[![Health](https://phpackages.com/badges/johannesschobel-laravel-userhistory/health.svg)](https://phpackages.com/packages/johannesschobel-laravel-userhistory)
```

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M138](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[ekino/newrelic-bundle

Integrate New Relic into Symfony2

28111.2M8](/packages/ekino-newrelic-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
