hanifhefaz/user-model-activity

Log activities over models into a log file.

1.0.0 2023-10-15 06:42 UTC

This package is auto-updated.

Last update: 2024-10-15 09:05:38 UTC


README

Introduction

Introducing the User Model Activity Package: Effortlessly Track Changes in Your Models

Have you ever found yourself in need of a simple yet effective way to track and log changes made to your Laravel models? Look no further! We are thrilled to present our User Model Activity package, designed to save the logs of model events such as creation, update, and deletion into a convenient log file. With this package, monitoring and auditing changes in your application's models has never been easier.

Installation

You can install the package with Composer.

composer require hanifhefaz/user-model-activity

After installation run the following command to publish the necessary assets and files:

php artisan vendor:publish --provider="Hanifhefaz\UserModelActivity\UserModelActivityServiceProvider"

That's it. You have successfully installed and published the required files and assets of the package.

Usage

Once you have installed and configured the package, you can start using it to track changes in your models. Follow this step-by-step guide to get started:

  • Logging File Configuration

    First things is first! Go to your config/logging.php and add the following to the channels array:

    /*
    * Logging.php
    */
        'channels' => [
            // existing code ...
    
            // 'stack' => [
            //     'driver' => 'stack',
            //     'channels' => ['single'],
            //     'ignore_exceptions' => false,
            // ],
    
            // ...
    
            'user-model-activity' => [
                'driver' => 'single',
                'path' => storage_path('logs/user-model-activity.log'),
                'level' => 'debug',
            ],
        ]

    Now, you can use the UserModelActivityLogger trait inside the model and all the logs will be tracked.

    <?php
    
    namespace App\Models;
    
    use Hanifhefaz\UserModelActivity\Traits\UserModelActivityLogger;
    use Illuminate\Database\Eloquent\Model;
    
    class Post extends Model
    {
        use UserModelActivityLogger;
        protected $fillable = ['title', 'content'];
    }

    That is it. Now just visit user-activity URL and select the file where you saved the logs. You will see all the logs.

    View

Conclusion

The User Model Activity package offers a straightforward and efficient solution for tracking changes made to your Laravel models. It simplifies the process of monitoring, auditing, and debugging model-related changes. Enhance your application's maintainability and gain valuable insights into your data with the User Model Activity package.

Contributions

Contributions are most welcome! Please pick one of the known issues and future plans or implement your own idea to help the package grow.

Current Contributers: Contributors.

Known issues and future plans

  • To further enhance the package, we plan to bring some changes in the log views. currently its not optimized.

  • A better approach to integrate user's details as well to the log. currently it will save details of model, and if your model has created_by it will be logged, but we need a better approach for this.

  • Parsing of the logs in a better way.

  • The trait is not working in spatie's Role and Permission models if they are extended from parent model such as below:

<?php

namespace App\Models;

use Hanifhefaz\UserModelActivity\Traits\UserModelActivityLogger;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Permission\Models\Permission as ParentPermission;

class Permission extends ParentPermission
{
    use HasFactory;
    use UserModelActivityLogger;

    protected $fillable = [
        'id','name','created_at','updated_at'
    ];

}

If you found any issue, please post it to issues section.