princeallan5's avatar

Using spatie Laravel-activitylog to track user logs only though apis and made in specific time

I'm working on a project using Spatie Laravel-activitylog Package to keep track of user logs for different activities in the project. Currently everything is being logged correctly. I would track different events including update, create, delete in different models within a specific time and only events through the api, i'm thinking of using a middleware but i'm stuck pn how to approach it. Kindly assist if you can

0 likes
3 replies
princeallan5's avatar

@aksvitpav here is my middleware, the problem im facing is obtaininig the model performedOn |

public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
    
        if (in_array('api', $request->route()->getAction('middleware')) && Auth::guard('api')->check()) {
            $model               = $request->route('model');
            $activityDescription = $this->getActivityDescription($request);

            activity()
                ->performedOn($model) // How to obtain this model ?? <---
                ->withProperties(['description' => $activityDescription])
                ->log('api_action');
        }

        return $response;
    }

1 like
princeallan5's avatar

I finally figured a work around. I added the model to the request in the respective controller $request->attributes->add(['model' => User::class])

1 like

Please or to participate in this conversation.