From d6e1fe2c5dbaf99daee60fd5059d04ef69f72e60 Mon Sep 17 00:00:00 2001 From: imadhigp Date: Thu, 13 Aug 2026 17:08:19 +0530 Subject: [PATCH] set admin --- .../Controllers/AdminDashboardController.php | 130 ++++- .../Controllers/StudentPortalController.php | 36 +- app/Models/Notification.php | 3 +- app/Models/StudentPortalLog.php | 21 +- ...reign_key_to_student_portal_logs_table.php | 25 +- resources/views/StudentPortal.blade.php | 59 ++ .../views/admin/admindashboard.blade.php | 526 +++++++++++++++++- resources/views/layouts/adminnav.blade.php | 2 +- .../views/layouts/studentportalnav.blade.php | 66 ++- resources/views/welcome.blade.php | 4 +- routes/web.php | 31 +- 11 files changed, 793 insertions(+), 110 deletions(-) diff --git a/app/Http/Controllers/AdminDashboardController.php b/app/Http/Controllers/AdminDashboardController.php index 961c557..716af41 100644 --- a/app/Http/Controllers/AdminDashboardController.php +++ b/app/Http/Controllers/AdminDashboardController.php @@ -3,36 +3,71 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Models\Timetable; +use App\Models\User; use App\Models\Notification; +use App\Models\Timetable; +use App\Models\StudentPortalLog; +use Exception; class AdminDashboardController extends Controller { public function index() { - if (!session()->has('admin_id')) { - return redirect()->route('admin.login')->with('error', 'Please login first.'); + try { + // 1. Fetch Users + $users = User::all(); + + // 2. Fetch Notifications + $notifications = Notification::orderBy('created_at', 'desc')->get(); + $unreadNotifications = Notification::where('is_read', false)->count(); + + // 3. Fetch Timetable Slots Count + $totalTimetableSlots = class_exists(Timetable::class) ? Timetable::count() : 0; + + // 4. Fetch Portal Logs with user eager loaded + $portalLogs = class_exists(StudentPortalLog::class) + ? StudentPortalLog::with('user')->get() + : []; + + return view('admin.AdminDashboard', compact( + 'users', + 'notifications', + 'unreadNotifications', + 'totalTimetableSlots', + 'portalLogs' + )); + + } catch (Exception $e) { + \Log::error('Admin Dashboard Error: ' . $e->getMessage()); + + if (config('app.debug')) { + throw $e; + } + + return back()->with('error', 'Something went wrong: ' . $e->getMessage()); } - - - $totalTimetableSlots = Timetable::count(); - - $unreadNotifications = Notification::where('target_role', 'admin') - ->where('is_read', false) - ->count(); - - $notifications = Notification::where('target_role', 'admin') - ->orderBy('created_at', 'desc') - ->take(5) - ->get(); - - return view('admin.admindashboard', compact( - 'totalTimetableSlots', - 'unreadNotifications', - 'notifications' - )); } + // Notifications Store + public function store(Request $request) + { + $request->validate([ + 'title' => 'required|string|max:255', + 'message' => 'required|string', + 'type' => 'required|in:info,success,warning', + ]); + + Notification::create([ + 'title' => $request->title, + 'message' => $request->message, + 'type' => $request->type, + 'is_read' => false, + ]); + + return redirect()->back()->with('success', 'Notification created successfully!'); + } + + // Mark as Read public function markAsRead($id) { $notification = Notification::findOrFail($id); @@ -40,4 +75,57 @@ class AdminDashboardController extends Controller return redirect()->back()->with('success', 'Notification marked as read!'); } + + // Update Notification + public function update(Request $request, $id) + { + $request->validate([ + 'title' => 'required|string|max:255', + 'message' => 'required|string', + 'type' => 'required|in:info,success,warning', + ]); + + $notification = Notification::findOrFail($id); + $notification->update($request->only(['title', 'message', 'type'])); + + return redirect()->back()->with('success', 'Notification updated successfully!'); + } + + // Delete Notification + public function destroy($id) + { + $notification = Notification::findOrFail($id); + $notification->delete(); + + return redirect()->back()->with('success', 'Notification deleted successfully!'); + } + + // Send User to Portal Log + public function sendToPortalLog($userId) + { + try { + $user = User::findOrFail($userId); + + // Check if user is already logged + $exists = StudentPortalLog::where('user_id', $user->id)->exists(); + if ($exists) { + return redirect()->back()->with('error', 'This user is already in Portal Logs!'); + } + + // Create new portal log + StudentPortalLog::create([ + 'user_id' => $user->id, + 'first_name' => $user->first_name ?? '', + 'last_name' => $user->last_name ?? '', + 'email' => $user->email, + 'phone' => $user->phone ?? null, + ]); + + return redirect()->back()->with('success', 'User successfully added to Portal Logs!'); + } catch (Exception $e) { + \Log::error('Send to Portal Log Error: ' . $e->getMessage()); + + return redirect()->back()->with('error', 'Failed to add user to Portal Log.'); + } + } } \ No newline at end of file diff --git a/app/Http/Controllers/StudentPortalController.php b/app/Http/Controllers/StudentPortalController.php index 804ce60..ec31d16 100644 --- a/app/Http/Controllers/StudentPortalController.php +++ b/app/Http/Controllers/StudentPortalController.php @@ -5,10 +5,24 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use App\Models\Notification; class StudentPortalController extends Controller -{ - +{public function index(Request $request) + { + + if (!session()->has('student_id') && !session()->has('student_log_id')) { + return redirect('/signin')->with('error', 'Please login first!'); + } + + + $notifications = Notification::latest()->take(10)->get(); + $unreadNotifications = Notification::where('is_read', 0)->count(); + + + return view('StudentPortal', compact('notifications', 'unreadNotifications')); + } + public function studentlogin(Request $request) { @@ -202,4 +216,22 @@ class StudentPortalController extends Controller return redirect()->back()->with('success', 'Profile updated successfully!'); } + + public function studentlogout(Request $request) + { + + $request->session()->forget(['student_id', 'student_log_id', 'student_name', 'student_email']); + $request->session()->invalidate(); + $request->session()->regenerateToken(); + + + if ($request->wantsJson() || $request->ajax()) { + return response()->json([ + 'success' => true, + 'message' => 'Logged out successfully!' + ], 200); + } + + return redirect('/')->with('success', 'Logged out successfully!'); + } } \ No newline at end of file diff --git a/app/Models/Notification.php b/app/Models/Notification.php index 213b241..859925a 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -17,4 +17,5 @@ class Notification extends Model 'is_read', 'target_role', ]; -} \ No newline at end of file +} + diff --git a/app/Models/StudentPortalLog.php b/app/Models/StudentPortalLog.php index 5676927..a51702d 100644 --- a/app/Models/StudentPortalLog.php +++ b/app/Models/StudentPortalLog.php @@ -2,24 +2,37 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class StudentPortalLog extends Model { - protected $table = 'student_portal_logs'; + use HasFactory; + + protected $table = 'student_portal_logs'; - protected $fillable = [ + 'user_id', + 'first_name', + 'last_name', 'email', + 'phone', 'studentid', 'password', 'pin', 'status', ]; - protected $hidden = [ 'password', 'pin', ]; -} + + /** + * Get the user associated with this portal log. + */ + public function user() + { + return $this->belongsTo(User::class, 'user_id'); + } +} \ No newline at end of file diff --git a/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php b/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php index 7007d8c..2d97a09 100644 --- a/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php +++ b/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php @@ -11,12 +11,18 @@ return new class extends Migration */ public function up(): void { - Schema::table('student_portal_logs', function (Blueprint $table) { - $table->foreignId('user_id') - ->nullable() - ->after('email') - ->constrained('users') - ->onDelete('cascade'); + Schema::create('student_portal_logs', function (Blueprint $table) { + $table->id(); + $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('cascade'); + $table->string('first_name')->nullable(); + $table->string('last_name')->nullable(); + $table->string('email'); + $table->string('phone')->nullable(); + $table->string('studentid')->nullable(); + $table->string('password')->nullable(); + $table->string('pin')->nullable(); + $table->string('status')->default('active'); + $table->timestamps(); }); } @@ -25,9 +31,6 @@ return new class extends Migration */ public function down(): void { - Schema::table('student_portal_logs', function (Blueprint $table) { - $table->dropForeign(['user_id']); - $table->dropColumn('user_id'); - }); + Schema::dropIfExists('student_portal_logs'); } -}; +}; \ No newline at end of file diff --git a/resources/views/StudentPortal.blade.php b/resources/views/StudentPortal.blade.php index f4d9243..4ae4226 100644 --- a/resources/views/StudentPortal.blade.php +++ b/resources/views/StudentPortal.blade.php @@ -321,6 +321,65 @@ + +
+
+
+
+ System Notifications +
+ {{ $unreadNotifications ?? 0 }} Unread +
+
+
    + @forelse($notifications ?? [] as $notification) +
  • +
    +
    + {{-- Type Badge --}} + @if($notification->type == 'success') + Success + @elseif($notification->type == 'warning') + Warning + @else + Info + @endif + +
    + {{ $notification->title }} +
    +

    {{ $notification->message }}

    + + + + {{ $notification->created_at->diffForHumans() }} + +
    + + {{-- Mark as Read Button --}} + @if(!$notification->is_read) +
    + @csrf + +
    + @else + + @endif +
    +
  • + @empty +
  • + + No notifications found. +
  • + @endforelse +
+
+
+
+
diff --git a/resources/views/admin/admindashboard.blade.php b/resources/views/admin/admindashboard.blade.php index 40aa857..c9147c0 100644 --- a/resources/views/admin/admindashboard.blade.php +++ b/resources/views/admin/admindashboard.blade.php @@ -4,6 +4,9 @@ @section('content') -
+
- +

Welcome to Admin Dashboard

Automobile Engineering Academy System Overview

-
- System Active -
+ + +
@@ -49,9 +70,15 @@
@endif + @if(session('error')) + + @endif +
-
@@ -71,7 +98,6 @@
-
@@ -91,7 +117,6 @@
-
@@ -111,7 +136,6 @@
-
@@ -132,10 +156,8 @@
- -
- - + +
@@ -186,37 +208,106 @@
-
-
- System Notifications -
- {{ $unreadNotifications ?? 0 }} Unread +
+
+ System Notifications +
+ {{ $unreadNotifications ?? 0 }} Unread +
+
-
+
    @forelse($notifications ?? [] as $notification) -
  • +
  • -
    -
    {{ $notification->title }}
    +
    + @if($notification->type == 'success') + Success + @elseif($notification->type == 'warning') + Warning + @else + Info + @endif + +
    + {{ $notification->title }} +

    {{ $notification->message }}

    - - {{ $notification->created_at->diffForHumans() }} + + + {{ $notification->created_at ? $notification->created_at->diffForHumans() : '' }}
    - @if(!$notification->is_read) -
    + +
    + @if(!$notification->is_read) + + @csrf + + + @else + + @endif + + + +
    @csrf -
    - @endif +
  • + + @empty
  • @@ -227,7 +318,384 @@
+
+ +
+
+
+
+
+ Registered System Users List +
+ {{ count($users ?? []) }} Users +
+
+
+ + + + + + + + + + + + + + @forelse($users ?? [] as $user) + + + + + + + + + + + @empty + + + + @endforelse + +
IDNameEmailPhonePortal PINCreated DateAction
#{{ $user->id }}{{ $user->first_name }} {{ $user->last_name }}{{ $user->email }}{{ $user->phone ?? 'N/A' }} +
+ @if(!empty($log->pin) && $log->pin !== 'N/A') + + + {{ $log->pin }} + + + + + + +
+ @csrf + @method('PUT') + + +
+ @else + + + N/A + + + + + @endif +
+
{{ $log->created_at ? $log->created_at->format('Y-m-d H:i') : 'N/A' }} +
+ @csrf + +
+
+ + No registered users found. +
+
+
+
+
+
+ + +
+
+
+
+
+ Student Portal Active Logs +
+
+ {{ count($portalLogs ?? []) }} Active Student Logs + + +
+
+
+
+ + + + + + + + + + + + + + + @forelse($portalLogs ?? [] as $log) + + + + + + + + + + + + + + + + + + @empty + + + + @endforelse + +
Log IDUser IDStudent NameEmailPhonePortal PINCreated DateActions
#LOG-{{ $log->id }}#{{ $log->user_id }} +
+ @if(!empty($log->first_name) || !empty($log->last_name)) + {{ trim($log->first_name . ' ' . $log->last_name) }} + @elseif($log->user) + {{ $log->user->name ?? trim($log->user->first_name . ' ' . $log->user->last_name) }} + @else + N/A + @endif +
+
{{ $log->email }}{{ $log->phone ?? ($log->user->phone ?? 'N/A') }} + + {{ $log->pin ?? 'N/A' }} + + {{ $log->created_at ? $log->created_at->format('Y-m-d H:i') : 'N/A' }} + + + Active + + +
+ + + + +
+ @csrf + @method('DELETE') + +
+
+
+ + No active student portal logs found. +
+
+
+
+
+
+ +
+ + + + + + + + + + @endsection \ No newline at end of file diff --git a/resources/views/layouts/adminnav.blade.php b/resources/views/layouts/adminnav.blade.php index 13b43ed..0faa8d6 100644 --- a/resources/views/layouts/adminnav.blade.php +++ b/resources/views/layouts/adminnav.blade.php @@ -7,7 +7,7 @@ @yield('title', 'Admin Portal - Automobile Academic') - +