<style>.woocommerce-product-gallery{ opacity: 1 !important; }</style>

MODULES

User Logs and Observers


What does it do?

This module logs all the actions by the users. Every time someone adds/updates/deletes an entry, it will log the action in database table user_actions, which will be shown in a separate menu item (see above).

For that, we use Model Observer functionality of Laravel.


How does the result look in QuickAdminPanel code?

We create a new database table with migration:

Schema::create('user_actions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('action');
    $table->string('action_model')->nullable();
    $table->integer('action_id')->nullable();
    $table->timestamps();
});
Then we create a new model called UserAction:
class UserAction extends Model
{
    protected $fillable = ['action', 'action_model', 'action_id', 'user_id'];


    /**
     * Set to null if empty
     * @param  $input
     */
    public function setUserIdAttribute($input)
    {
        $this->attributes['user_id'] = $input ? $input : null;
    }

    /**
     * Set attribute to money format
     * @param  $input
     */
    public function setActionIdAttribute($input)
    {
        $this->attributes['action_id'] = $input ? $input : null;
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

}
Then we create one new Observer class, with this code.
app/Observers/UserActionsObserver.php
class UserActionsObserver
{
    public function saved($model)
    {
        if ($model->wasRecentlyCreated == true) {
            // Data was just created
            $action = 'created';
        } else {
            // Data was updated
            $action = 'updated';
        }
        if (Auth::check()) {
            UserAction::create([
                'user_id'      => Auth::user()->id,
                'action'       => $action,
                'action_model' => $model->getTable(),
                'action_id'    => $model->id
            ]);
        }
    }

    public function deleting($model)
    {
        if (Auth::check()) {
            UserAction::create([
                'user_id'      => Auth::user()->id,
                'action'       => 'deleted',
                'action_model' => $model->getTable(),
                'action_id'    => $model->id
            ]);
        }
    }
}
Then we attach this observer for every new CRUD model created: app/Course.php:
class Course extends Model
{

    public static function boot()
    {
        parent::boot();

        Course::observe(new \App\Observers\UserActionsObserver);
    }

    ...
Finally, we create a CRUD called User actions to just view the table.
resources/views/admin/user_actions/index.blade.php
<table class="table datatable">
    <thead>
        <tr>
            <th>@lang('global.user-actions.created_at')</th>
            <th>@lang('global.user-actions.fields.user')</th>
            <th>@lang('global.user-actions.fields.action')</th>
            <th>@lang('global.user-actions.fields.action-model')</th>
            <th>@lang('global.user-actions.fields.action-id')</th>
        </tr>
    </thead>

    <tbody>
        @if (count($user_actions) > 0)
            @foreach ($user_actions as $user_action)
                <tr>
                    <td>{{ $user_action->created_at or '' }}</td>
                    <td>{{ $user_action->user->name or '' }}</td>
                    <td>{{ $user_action->action }}</td>
                    <td>{{ $user_action->action_model }}</td>
                    <td>{{ $user_action->action_id }}</td>
                </tr>
            @endforeach
        @else
            <tr>
                <td colspan="5">@lang('global.app_no_entries_in_table')</td>
            </tr>
        @endif
    </tbody>
</table>

You can easily customize the module after download by adding more fields or more logic in the files above.


How to install/use the module?

All you need to do is go to your panel's Modules menu item, find the module in the list and click Install.

You don't need to change any other settings or click anything - user logging will automatically start working for all CRUDs after module installation.


More information