Laravel Custom User Log Activity Example Tutorial

By Hardik Savani November 5, 2023 Category : Laravel

In this tutorial we will learn how to create custom user log activity without composer page in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 application.

In this article, I share with you very simple and easy log management in laravel application. I created custom log tables and helper facade that way you can keep your track user logs. You can simply modify table structure and also helper method name etc as you like. I always like to do something new so i did very simple user custom log activity management system.

After reviewing all codes, you can understand how it works and simple. we will manage log without using anymore package. In this example i created "log_activities" table with following column as listed bellow:










As listed above fields. I always put on log, so you can simply modify table structure and add new fields. I create LogActivity helper for put on log helper. So just follow few step and get very basic example of keep log activity.

After creating successfully this example, you will have layout as like bellow screen shot.


Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

In this step we have to make database configuration for example database name, username, password etc. So let's open .env file and fill all details like as bellow:





DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Step 3: Create LogActivity Table and Model

In this step we have to create migration for log_activities table using Laravel 5.4 php artisan command, so first fire bellow command:

php artisan make:migration create_log_activity_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create contactus table.


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateLogActivityTable extends Migration



* Run the migrations.


* @return void


public function up()


Schema::create('log_activities', function (Blueprint $table) {












* Reverse the migrations.


* @return void


public function down()





Now run above migration by following command:

php artisan migrate

After creating table we have to create model for "log_activities" table so just run bellow command and create new model:

php artisan make:model LogActivity

Ok, so after run bellow command you will find app/LogActivity.php and put bellow content in LogActivity.php file:



namespace App;

use Illuminate\Database\Eloquent\Model;

class LogActivity extends Model



* The attributes that are mass assignable.


* @var array


protected $fillable = [

'subject', 'url', 'method', 'ip', 'agent', 'user_id'



Step 4: Create LogActivity Helper Class

In this step we will create new directory "Helpers" in App directory. After created Helpers folder we require to create create LogActivity.php file and put bellow code:



namespace App\Helpers;

use Request;

use App\LogActivity as LogActivityModel;

class LogActivity


public static function addToLog($subject)


$log = [];

$log['subject'] = $subject;

$log['url'] = Request::fullUrl();

$log['method'] = Request::method();

$log['ip'] = Request::ip();

$log['agent'] = Request::header('user-agent');

$log['user_id'] = auth()->check() ? auth()->user()->id : 1;



public static function logActivityLists()


return LogActivityModel::latest()->get();



Step 5: Register Helper Class

In this step we have to register Helper class as facade in configuration file. So let's open app.php file and add Helper facade with load class. open app.php file and add bellow Helper facade line.



'aliases' => [


'LogActivity' => App\Helpers\LogActivity::class,


Step 6: Add Route

In this step we will add two new routes. i added "add-to-log" route for testing you can simply use addToLog() with subject title like you can add "User created Successfully", "User updated successfully" etc subject as argument. another route for listing on user activity logs what was did by user. So let's add bellow two route.


Route::get('add-to-log', 'HomeController@myTestAddToLog');

Route::get('logActivity', 'HomeController@logActivity');

Step 7: Add Controller Method

In this step, we will add new two methods in HomeController file. i write how to add log on logs table and listing. So let's add bellow code.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller



* Create a new controller instance.


* @return void


public function __construct()




* Show the application dashboard.


* @return \Illuminate\Http\Response


public function myTestAddToLog()


\LogActivity::addToLog('My Testing Add To Log.');

dd('log insert successfully.');



* Show the application dashboard.


* @return \Illuminate\Http\Response


public function logActivity()


$logs = \LogActivity::logActivityLists();

return view('logActivity',compact('logs'));



Step 8: Add View File

In last step, we will create logActivity.blade.php file for display all logs with details form table. So let's copy from bellow code and put.


<!DOCTYPE html>



<title>Log Activity Lists</title>

<link rel="stylesheet" href="" />



<div class="container">

<h1>Log Activity Lists</h1>

<table class="table table-bordered">







<th width="300px">User Agent</th>

<th>User Id</th>




@foreach($logs as $key => $log)


<td>{{ ++$key }}</td>

<td>{{ $log->subject }}</td>

<td class="text-success">{{ $log->url }}</td>

<td><label class="label label-info">{{ $log->method }}</label></td>

<td class="text-warning">{{ $log->ip }}</td>

<td class="text-danger">{{ $log->agent }}</td>

<td>{{ $log->user_id }}</td>

<td><button class="btn btn-danger btn-sm">Delete</button></td>








Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:



I hope it can help you...