Файловый менеджер - Редактировать - /home/clickysoft/public_html/travel-guru.clickysoft.net/app/Repositories/UserChatRepository.php
Назад
<?php namespace App\Repositories; use App\Interfaces\UserChatRepositoryInterface; use App\Models\User; use App\Models\UserChat; use Illuminate\Support\Facades\Auth; class UserChatRepository implements UserChatRepositoryInterface { /** * Store a chat message along with file (if any). * * @param array $data * @return \App\Models\UserChat */ public function storeMessage(array $data) { // Create a new message instance $message = new UserChat(); $message->sender_id = Auth::id(); $message->receiver_id = $data['receiver_id']; $message->message = $data['message'] ?? null; $message->status = 'unread'; // Handle file upload if present if (isset($data['file'])) { $file = $data['file']; $originalFileName = $file->getClientOriginalName(); // Generate a random directory name $randomDir = 'chat_files/' . rand(100, 999); // Store file with original name inside the random directory $filePath = $file->storeAs($randomDir, $originalFileName, 'public'); $fileType = $file->getClientMimeType(); $message->file_name = $originalFileName; $message->file_path = $filePath; $message->file_type = $fileType; } $message->save(); return $message; } /** * Retrieve a list of users that the authenticated user has chatted with, * along with the last message, file information, and message time. * * @param int $authUserId The ID of the authenticated user. * @return \Illuminate\Database\Eloquent\Collection A collection of users who have had chats with the authenticated user, including details of the last message. */ public function getUserChats($authUserId) { return User::select('users.*', 'user_chats.message', 'user_chats.created_at as last_message_time', 'user_chats.file_name', 'user_chats.file_type', 'user_chats.file_path', 'user_chats.sender_id', 'user_chats.id as chat_id' ) ->leftJoin('user_chats', function ($join) { $join->on('users.id', '=', 'user_chats.sender_id') ->orOn('users.id', '=', 'user_chats.receiver_id'); }) ->where(function ($query) use ($authUserId) { $query->where('user_chats.sender_id', $authUserId) ->orWhere('user_chats.receiver_id', $authUserId); }) ->where('users.id', '!=', $authUserId) ->groupBy('users.id') ->orderByRaw('chat_id DESC') ->get(); } public function markMessagesAsRead($authUserId, $userId) { return UserChat::where('receiver_id', $authUserId) ->where('sender_id', $userId) ->where('status', 'unread') ->update(['status' => 'read']); } public function getMessagesBetweenUsers($authUserId, $selectedUserId) { return UserChat::oneToOneChat($authUserId, $selectedUserId) ->orderBy('id', 'asc') ->get(); } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка