When you're working with laravel application there's seemingly a lot going on. Users log in and log out, create, update and delete content, emails get sent, and so on. Recently I've been confronted with the spatie/laravel-activitylog Package that provides straightforward functions to log the activities of the users of your application. It can also automatically log model events. All the activities will be saved in the activity_log table.
This package needs PHP 7.1+ and Laravel 5.2 or higher.
The latest version of this package needs PHP 8.0+ and Laravel 8 or higher. If you're employing an older version of the Laravel framework, Then you can utilize v3, v2, or v1 of this package.
For the installation purpose, you've to run this command:
composer require spatie/laravel-activitylog
The service provider will be automatically registered. If you need to store your activities in another database connection, you can specify ACTIVITY_LOGGER_DB_CONNECTION in your .env file.
After that, you've to clear the application config cache:
php artisan config:clear
You've to publish the migration with this command:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
Next, you've to generate the activity_log table by using this command:
php artisan migrate
Here's an example of how you can log some activity:
activity()->log('Hey, I logged something');
It will generate a record in the activity_log table. You can recover all activity utilizing the Spatie\Activitylog\Models\Activity model handed by the laravel-activitylog package.
Here's another advanced way of doing this:
activity() ->performedOn($anEloquentModel) ->causedBy($user) ->withProperties(['customProperty' => 'customValue']) ->log('Look mum, I logged something'); $lastLoggedActivity = Activity::all()->last(); $lastLoggedActivity->subject; //returns an instance of an eloquent model $lastLoggedActivity->causer; //returns an instance of your user model $lastLoggedActivity->getExtraProperty('customProperty'); //returns 'customValue' $lastLoggedActivity->description; //returns 'Look mum, I logged something'
This package is pretty cool, it can also automatically log model events. For Example:
$newsItem->name = 'updated name'; $newsItem->save(); //updating the newsItem will induce an activity being logged $activity = Activity::all()->last(); $activity->description; //returns 'updated' $activity->subject; //returns the instance of NewsItem that was generated
When you Call $activity->changes. It will return the following array:
[ 'attributes' => [ 'name' => 'original name', 'text' => 'Lorum Ipsum', ], 'old' => [ 'name' => 'updated name', 'text' => 'Lorum Ipsum', ], ];
If you want to know more about this package, you can head over to the documentation to learn all the options.
Published at : 30-07-2021
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.