Laravel 11 Custom User Logs Activity Example

Hello, laravel web developer! In this article, we'll see how to create custom user logs activity in laravel 11. Here, we'll create activity logs in laravel 11 without a package. Previously we'd use spatie/laravel-activitylog. 

We'll create custom log tables and a helper facade to track user logs. Also, you can customize it as per your requirements.

Laravel 11 Custom User Logs Activity

Laravel 11 Custom User Logs Activity


Step 1: Install Laravel 11

In this step, we'll install laravel 11 using the following command.

composer create-project --prefer-dist laravel/laravel user-activity-logs


Step 2: Configure Database

Then, configure the database in the .env file.





Step 3: Create LogActivity Table and Model

Next, create a migration and model using the following command.

php artisan make:migration create_log_activity_table


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
     * Run the migrations.
     * @return void
    public function up(): void
        Schema::create('log_activities', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down(): void

Then, migrate the table into the database using the following command.

php artisan migrate

Next, create a model using the following command.

php artisan make:model LogActivity



namespace App\Models;

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

Then, create a LogActivity helper class and define the logic of user activity.



namespace App\Helpers;
use Request;
use App\Models\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

Next, register the Helper class as a facade in the configuration file. So, let's open app.php and define the facades.


'aliases' => [


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

Step 6: Define Route

Now, define the routes into the web.php file.


Route::get('add-to-log', 'LogController@addToLog');
Route::get('activity-logs', 'LogController@activityLog');
Step 7: Crete Controller

Next, define the function in the LogController.php. So, add the following code to that file.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LogController extends Controller
     * Create a new controller instance.
     * @return void
    public function __construct()

     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function addToLog()
        \LogActivity::addToLog('Add To Log Test');
        dd('log insert successfully.');

     * Show the application dashboard.
     * @return \Illuminate\Http\Response
    public function activityLog()
        $logs = \LogActivity::logActivityLists();
        return view('activity-logs',compact('logs'));
Step 8: Create View File

Then, create a blade file to display activity logs.


<!DOCTYPE html>
	<title>Laravel 11 Custom User Logs Activity Example - Techsolutionstuff</title>
	<link rel="stylesheet" href="" />

<div class="container">
	<h1>Laravel 11 Custom User Logs Activity Example - Techsolutionstuff</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>



Step 9: Run Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve


You might also like :


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.