Laravel

How to Track All User Activity in Laravel Application?

Hello Laravel Friends,

In this blog, we will explore how to implement user activity tracking in a Laravel application to gain better insights and enhance user experience.

As the popularity of web applications continues to soar, understanding user behavior and interactions within your Laravel application becomes paramount. Tracking user activity not only provides valuable insights into user preferences but also helps debug and ensure your application’s security.

Let’s see how you can track user activity in Laravel.

Steps to Track All User Activity in Laravel Application:

Step 1:  Create a Laravel project using the below command

composer create-project laravel/laravel:^9.0 laravel-blog

Step 2: To get all activity logs, we use the spatie/laravel-medialibrary

composer require "spatie/laravel-medialibrary:^10.0.0"

Step 3: You need to publish the migration to create the media table using the below command

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" –tag="migrations"

Step 4: After that, you need to run the migration command

php artisan migrate

Step 5: Now, publish the config File using the below command 

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" –tag="config"

Step 6: You can log activity like the below code 

public function userCreate(Request $request)
{
        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->save();
        
        activity('create')
        ->performedOn($user) // Entry add in table. model name(subject_type) & id(subject_id)
        ->causedBy(Auth::user()) //causer_id = admin id, causer type = admin model
        ->log('User Created By ' . Auth::user()->name);

        return redirect()->route('user-index')->with('success', "User Create Successfully");
}

Step 7: Get All activity using the below code

<?php

namespace App\Http\Controllers;
use Spatie\Activitylog\Models\Activity;

class ActivityController extends Controller
{
 public function activityLogsList()
   {
      $activityLogData = Activity::with('causer')->get();
      return view('activity-logs', compact('activityLogData'));
   }
}

Output:

Conclusion:

This way, you can track all user activity in the Laravel application. If you cannot track user activity log in Laravel successfully, you can take help from an experienced Laravel Developer.

Share the article with your friends and stay updated for more Laravel solutions.

Happy Coding!

Click to rate this post!
[Total: 12 Average: 2.3]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Node.js | HTTP Module

Node.js, known for its asynchronous and event-driven architecture, offers a multitude of built-in modules that…

2 days ago

Google’s August 2024 Core Update has Fully Rolled Out

Google has officially rolled out its much-anticipated August 2024 Core Update on August 15, 2024,…

3 days ago

Invoicing Guidelines for Independent E-Commerce Businesses

In e-commerce, it's important to understand that it's not just about running an online store.…

4 days ago

Building Dynamic Frontend Applications with Laravel and Alpine.js

In modern web development, building dynamic, interactive front-end applications is essential. Laravel, a powerful PHP…

4 days ago

Exploring Laravel 10’s New Query Builder Enhancements

Laravel, known for its elegant syntax and ease of use, has continually refined its core…

4 days ago

Magento 2 Extensions Digest August 2024 (New Release & Updates)

As we continue to innovate and enhance the Magento 2 ecosystem, August 2024 has been…

6 days ago