๐Ÿ”’ Closed Bot test

Status
Not open for further replies.
Yes, I can definitely help you with that. Here's a step-by-step guide to create user activity logs using Laravel:

Step 1: Create a new Laravel project by running the following command in your terminal:
Code:
composer create-project --prefer-dist laravel/laravel user-activity-logs

Step 2: Set up a database connection in your .env file by adding the following lines:
Code:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=user_activity_logs
DB_USERNAME=root
DB_PASSWORD=

Step 3: Create a new migration for the user activity logs by running the following command in your terminal:
Code:
php artisan make:migration create_user_activity_logs_table

Step 4: In the new migration file, you can define the structure of your user activity logs table. For example:
Code:
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUserActivityLogsTable extends Migration
{
    public function up()
    {
        Schema::create('user_activity_logs', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('activity');
            $table->text('details')->nullable();
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    public function down()
    {
        Schema::dropIfExists('user_activity_logs');
    }
}

Step 5: Run the migration to create the user activity logs table by running the following command in your terminal:
Code:
php artisan migrate

Step 6: Create a new middleware that will log user activity. You can create a middleware file named "LogUserActivity.php" in the app/Http/Middleware directory with the following code:
Code:
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class LogUserActivity
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (Auth::check()) {
            $user = Auth::user();
            $activity = $request->route()->getActionName();

            Log::channel('user_activity')->info($user->name . ' has accessed ' . $activity);
        }

        return $response;
    }
}

Step 7: Register the middleware in the app/Http/Kernel.php file by adding the following line to the $routeMiddleware array:
Code:
'log.user.activity' => \App\Http\Middleware\LogUserActivity::class,

Step 8: Add the middleware to the routes that you want to log user activity for. For example:
Code:
Route::group(['middleware' => ['auth', 'log.user.activity']], function () {
    // your routes here
});

Step 9: Lastly, configure the logging settings in your config/logging.php file. You can add a new channel for the user activity logs with the following code:
Code:
'user_activity' => [
    'driver' => 'daily',
    'path' => storage_path('logs/user_activity.log'),
    'level' => 'info',
    'days' => 7,
],

That's it! Your Laravel application should now be logging user activity to the user_activity.log file in your storage/logs directory.
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 295
    Views
  • 1
    Participants
Last reply from:
trumpoikot

Online now

Members online
1,007
Guests online
1,095
Total visitors
2,102

Forum statistics

Threads
2,276,161
Posts
28,967,972
Members
1,231,145
Latest member
kennethCASTRO2012
Back
Top