@php $staffCache = []; $studentCache = []; $participantCache = []; $schoolAdminId = null; @endphp @foreach($messages as $msg) @php $isMe = ($msg->sender_id == $staffId); // ============ 1) PARTICIPANTS CACHE ============ if (!isset($participantCache[$msg->conversation_id])) { $participantCache[$msg->conversation_id] = DB::table('school_conversation_participants') ->where('conversation_id', $msg->conversation_id)->get(); } // ============ 2) ✅ SCHOOL ADMIN ID via users.user_type (NOT role!) ============ if ($schoolAdminId === null) { $schoolAdminId = (int) (DB::table('users') ->where('school_id', session('staff_school_id')) ->where('user_type', 'school_admin') ->value('id') ?? 0); } // ============ 3) SENDER ROLE — COLLISION-PROOF ORDER ============ $senderRole = null; // (a) ADMIN FIRST: users.id 4 collides with school_staff.id 4 (JASPREET) if (!$isMe && $schoolAdminId && (int) $msg->sender_id === $schoolAdminId) { $senderRole = 'school'; } // (b) Participants: STAFF wins, then STUDENT, then SCHOOL last. // (old buggy chats contain a fake 'school' participant user_id=1 which // equals GURLEEN's staff id — so 'school' must be checked LAST here) if ($senderRole === null) { $roles = []; foreach ($participantCache[$msg->conversation_id] as $part) { if ((int) $part->user_id === (int) $msg->sender_id) { $roles[] = $part->role; } } if (in_array('staff', $roles)) { $senderRole = 'staff'; } elseif (in_array('student', $roles)) { $senderRole = 'student'; } elseif (in_array('school', $roles)) { $senderRole = 'school'; } } // (c) NOT a participant (Principal / Admin in old chats): staff -> student -> school if ($senderRole === null && !$isMe) { if (!isset($staffCache[$msg->sender_id])) { $staffCache[$msg->sender_id] = DB::table('school_staff') ->where('id', $msg->sender_id) ->where('school_id', session('staff_school_id')) ->first(); } if ($staffCache[$msg->sender_id]) { $senderRole = 'staff'; } else { if (!isset($studentCache[$msg->sender_id])) { $studentCache[$msg->sender_id] = DB::table('students') ->where('id', $msg->sender_id) ->where('school_id', session('staff_school_id')) ->first(); } $senderRole = $studentCache[$msg->sender_id] ? 'student' : 'school'; } } if ($senderRole === null) { $senderRole = 'school'; } // ============ 3) SENDER NAME / INITIALS / DESIGNATION ============ $senderName = null; $senderInitials = ''; $senderDesignation = ''; if (!$isMe) { if ($senderRole == 'student') { if (!isset($studentCache[$msg->sender_id])) { $studentCache[$msg->sender_id] = DB::table('students')->where('id', $msg->sender_id)->first(); } $senderName = $studentCache[$msg->sender_id]->student_name ?? 'Student'; } elseif ($senderRole == 'staff') { if (!isset($staffCache[$msg->sender_id])) { $staffCache[$msg->sender_id] = DB::table('school_staff')->where('id', $msg->sender_id)->first(); } $senderName = $staffCache[$msg->sender_id]->staff_name ?? 'Teacher'; $senderDesignation = strtoupper(trim($staffCache[$msg->sender_id]->designation ?? '')); } else { $senderName = 'School Admin'; $senderDesignation = 'ADMIN'; } foreach (explode(' ', $senderName) as $p) { $senderInitials .= strtoupper(substr($p, 0, 1)); } } // ✅ PRINCIPAL messages = SCHOOL ADMIN bubble for students & teachers $showAsSchool = (!$isMe) && ($senderRole == 'school' || ($senderRole == 'staff' && $senderDesignation === 'PRINCIPAL')); $isStudent = (!$isMe) && $senderRole == 'student'; $isTeacher = (!$isMe) && $senderRole == 'staff' && !$showAsSchool; $darkBubble = $isMe || $showAsSchool; @endphp
{{ $showAsSchool ? 'School Admin' : $senderName }}@if($isTeacher) · Teacher @endif
@endif{{ $fileName }}
Click to download