Skip to content

Commit

Permalink
Merge pull request #51 from AmanRawat2001/User-Activity-Log
Browse files Browse the repository at this point in the history
adding the activity logs of the user in the User Controller only
  • Loading branch information
WailanTirajoh authored Sep 24, 2024
2 parents 931781f + d323c09 commit 6361759
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 9 deletions.
26 changes: 26 additions & 0 deletions app/Http/Controllers/ActivityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Spatie\Activitylog\Models\Activity;

class ActivityController extends Controller
{
//
public function index()
{
$activities = Activity::paginate(10);

return view('activity_log.index', compact('activities'));
}
public function all()
{
$activities = Activity::all();

return view('activity_log.all', compact('activities'));
}

}


1 change: 1 addition & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AuthController extends Controller
public function postLogin(PostLoginRequest $request)
{
if (Auth::attempt($request->only('email', 'password'))) {
activity() ->causedBy(auth()->user())->log('User logged into the portal'); // Log activity message
return redirect('dashboard')->with('success', 'Welcome '.auth()->user()->name);
}

Expand Down
12 changes: 8 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ public function create()

public function store(StoreUserRequest $request)
{
activity()->causedBy(auth()->user())->log('User ' . $request->name . ' created');
$user = $this->userRepository->store($request);

return redirect()->route('user.index')->with('success', 'User '.$user->name.' created');
return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' created');
}

public function show(User $user)
{
activity()->causedBy(auth()->user())->log('User ' . $user->name . ' viewed');
if ($user->role === 'Customer') {
$customer = Customer::where('user_id', $user->id)->first();

Expand All @@ -62,6 +64,7 @@ public function edit(User $user)

public function update(User $user, UpdateCustomerRequest $request)
{
activity()->causedBy(auth()->user())->log('User ' . $user->name . ' updated');
$user->update($request->all());

if ($user->isCustomer()) {
Expand All @@ -70,17 +73,18 @@ public function update(User $user, UpdateCustomerRequest $request)
]);
}

return redirect()->route('user.index')->with('success', 'User '.$user->name.' udpated!');
return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' udpated!');
}

public function destroy(User $user)
{
activity()->causedBy(auth()->user())->log('User ' . $user->name . ' updated');
try {
$user->delete();

return redirect()->route('user.index')->with('success', 'User '.$user->name.' deleted!');
return redirect()->route('user.index')->with('success', 'User ' . $user->name . ' deleted!');
} catch (\Exception $e) {
return redirect()->route('user.index')->with('failed', 'Customer '.$user->name.' cannot be deleted! Error Code:'.$e->errorInfo[1]);
return redirect()->route('user.index')->with('failed', 'Customer ' . $user->name . ' cannot be deleted! Error Code:' . $e->errorInfo[1]);
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"laravel/framework": "^11.0",
"laravel/reverb": "@beta",
"laravel/tinker": "^2.5",
"pusher/pusher-php-server": "^7.2"
"pusher/pusher-php-server": "^7.2",
"spatie/laravel-activitylog": "^4.8"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.13",
Expand Down
155 changes: 153 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions config/activitylog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

return [

/*
* If set to false, no activities will be saved to the database.
*/
'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),

/*
* When the clean-command is executed, all recording activities older than
* the number of days specified here will be deleted.
*/
'delete_records_older_than_days' => 365,

/*
* If no log name is passed to the activity() helper
* we use this default log name.
*/
'default_log_name' => 'default',

/*
* You can specify an auth driver here that gets user models.
* If this is null we'll use the current Laravel auth driver.
*/
'default_auth_driver' => null,

/*
* If set to true, the subject returns soft deleted models.
*/
'subject_returns_soft_deleted_models' => false,

/*
* This model will be used to log activity.
* It should implement the Spatie\Activitylog\Contracts\Activity interface
* and extend Illuminate\Database\Eloquent\Model.
*/
'activity_model' => \Spatie\Activitylog\Models\Activity::class,

/*
* This is the name of the table that will be created by the migration and
* used by the Activity model shipped with this package.
*/
'table_name' => 'activity_log',

/*
* This is the database connection that will be used by the migration and
* the Activity model shipped with this package. In case it's not set
* Laravel's database.default will be used instead.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION'),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

class CreateActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->create(config('activitylog.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject', 'subject');
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->timestamps();
$table->index('log_name');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->dropIfExists(config('activitylog.table_name'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

class AddEventColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('event');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

class AddBatchUuidColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}

public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}
2 changes: 1 addition & 1 deletion public/hot
Original file line number Diff line number Diff line change
@@ -1 +1 @@
http://127.0.0.1:5173
http://[::1]:5173
Loading

0 comments on commit 6361759

Please sign in to comment.