How to implement eloquent events in laravel using commmon Trait for all models

强颜欢笑 提交于 2019-12-05 16:21:51

I found the solution, all ok with Model and Migration, it was the trait where it was making issue. There were a no. of things wrong and which was preventing it to work properly.

And the most important thing which was wrong is the Log, I did;t included proper class for it and that caused the issue.

Here is the corrected code for the trait file only.

<?php

namespace App\Traits;

use Auth;
use Request;
use App\Master\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

/**
 * Class ModelEventLogger
 * @package App\Traits
 *
 *  Automatically Log Add, Update, Delete events of Model.
 */
trait ActivityLogger {

    /**
     * Automatically boot with Model, and register Events handler.
     */
    protected static function bootActivityLogger()
    {   
        foreach (static::getRecordActivityEvents() as $eventName) {
            static::$eventName(function ($model) use ($eventName) {
                try {
                    $reflect = new \ReflectionClass($model);
                    return Activity::create([
                        'user_id' => Auth::id(),
                        'content_id' => $model->attributes[$model->primaryKey],
                        'content_type' => get_class($model),
                        'action' => static::getActionName($eventName),
                        'description' => ucfirst($eventName) . " a " . $reflect->getShortName(),
                        'details' => json_encode($model->getDirty()),
                        'ip_address' => Request::ip()
                    ]);
                } catch (\Exception $e) {
                    Log::debug($e->getMessage());
                }
            });
        }
    }

    /**
     * Set the default events to be recorded if the $recordEvents
     * property does not exist on the model.
     *
     * @return array
     */
    protected static function getRecordActivityEvents()
    {
        if (isset(static::$recordEvents)) {
            return static::$recordEvents;
        }

        return [
            'created',
            'updated',
            'deleted',
            'restored'
        ];
    }

    /**
     * Return Suitable action name for Supplied Event
     *
     * @param $event
     * @return string
     */
    protected static function getActionName($event)
    {
        switch (strtolower($event)) {
            case 'created':
                return 'create';
                break;
            case 'updated':
                return 'update';
                break;
            case 'deleted':
                return 'delete';
                break;
            case 'restored':
                return 'restore';
                break;
            default:
                return 'unknown';
        }
    }
} 

Please check and advise if anything wrong or could be done in better way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!