Laravel 6 - How to Log All URL's of User Visited
Asked Answered
M

3

5

I want to log all URL's that user visited. But there's not the url i want to logged from my code. Here are the code that im done, please give me some advise. Thanks and appreciate.

Web Routes

Route::get('/{url}', 'LogController@myTestAddToLog')->where('url', '[\w\d\-]+(.*)');

Log Controller

public function myTestAddToLog()
{
    \LogActivity::addToLog('My Testing Add To Log.');
}

App/Helpers LogActivity

public static function addToLog($subject)
{
    $log = [];
    $log['url'] = Request::fullUrl();
    $log['ip'] = Request::ip();
    $log['user_id'] = auth()->check() ? auth()->user()->id : 1;
    LogActivityModel::create($log);
}
Mcclimans answered 20/1, 2020 at 4:6 Comment(1)
Create a middle ware and use in kernel.php fileRadiculitis
D
10

You need to create a middleware.

php artisan make:middleware AddToLog

And then, put your code inside the middleware.

<?php

namespace App\Http\Middleware;

use Closure;
use App\LogActivityModel;

class AddToLog
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if(auth()->user()) {
            LogActivityModel::create([
                'url' => request()->fullUrl(),
                'ip' => request()->ip(),
                'user_id' => auth()->id(),
            ]);
        }

        return $response;
    }
}

Then, inside app/Http/kernel.php, add your middleware to $middleware. This would allow any URL called would trigger this code.

protected $middleware = [
    ...
    \App\Http\Middleware\AddToLog::class,
];
Dearth answered 20/1, 2020 at 4:21 Comment(5)
hi @Adlan, i can't get my user_id. It's NULL.Mcclimans
Can you verify you are indeed calling the URL while logged in? You can try to log auth()->user() and see what you get.Dearth
I'm getting NULLMcclimans
@Mcclimans That means your are not logged in. Please check again. Btw, I already put if-else condition to log ONLY when user is logged in, so you won't get null result in your DB.Dearth
Yup, but i'm sure logged in and i'm using dd('Auth::user()->id') for testing. Btw, i solved it! $next($request) must declare before create data. Thanks for your solution!Mcclimans
G
3

For those who still has the issue I had modified a bit the answer provided by @Adlan and based on the comments of his answer. Since you will be logging the activities after the request has been performed, the accepted answer needs to be modified. Following is the modified code.

    public function handle($request, Closure $next)
{

    $response = $next($request);

    if(auth()->user()) {
        Activity::create([
            'url' => request()->fullUrl(),
            'ip_address' => request()->ip(),
            'user_id' => auth()->id(),
        ]);

    }

    return $response;

}

First, the response for the next request is saved. Then user authentication is checked and finally, the response is returned.

Reference : https://laravel.com/docs/5.8/middleware

https://mcmap.net/q/1889972/-laravel-6-how-to-log-all-url-39-s-of-user-visited

Goldbrick answered 14/2, 2020 at 5:46 Comment(0)
H
1

Please use the below code to log all url that user has visited

Log::info(url()->full()); // For just loggin

And for storing user log create table store the url with timestamp and IP

Hengel answered 20/1, 2020 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.