diff --git a/app/Http/Controllers/EmployerController.php b/app/Http/Controllers/EmployerController.php index 5cb2972..355a7d2 100644 --- a/app/Http/Controllers/EmployerController.php +++ b/app/Http/Controllers/EmployerController.php @@ -534,11 +534,10 @@ class EmployerController extends Controller return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]) ->withErrors($validator); } else { - Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); + $new_message = Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); //return redirect()->route('employer.dialog', ['user1' => $user1, 'user2' => $user2]); - return redirect()->route('employer.dialog', - ['user1' => $user1, 'user2' => $user2, 'ad_employer' => $id_vacancy, 'ad_name' => $ad_name]); + return redirect()->route('employer.dialog', ['chat' => $new_message->chat_id_from]); } } diff --git a/app/Http/Controllers/WorkerController.php b/app/Http/Controllers/WorkerController.php index 48028bc..e7b9412 100644 --- a/app/Http/Controllers/WorkerController.php +++ b/app/Http/Controllers/WorkerController.php @@ -817,9 +817,7 @@ class WorkerController extends Controller $Messages = Chat::get_chat_messages($chat); - Message::where('user_id', '=', $chat->to_user_id) - ->where('to_user_id', '=', $chat->user_id) - ->update(['flag_new' => 0]); + Message::where('chat_id_to', '=', $chat->id)->update(['flag_new' => 0]); return view('workers.dialog', compact('companion', 'sender', 'chat', 'Messages', 'ad_employer')); } @@ -1062,8 +1060,7 @@ class WorkerController extends Controller } else { $new_message = Message::add_message($request, $user1, $user2, $request->all(), file_store_path: 'messages'); - return redirect()->route('worker.dialog', - ['user1' => $user1, 'user2' => $user2, 'chat' => $new_message->chat_id_from]); + return redirect()->route('worker.dialog', ['chat' => $new_message->chat_id_from]); } } diff --git a/app/Models/Chat.php b/app/Models/Chat.php index b0f6c35..7d6a0dd 100644 --- a/app/Models/Chat.php +++ b/app/Models/Chat.php @@ -42,6 +42,11 @@ class Chat extends Model return $this->hasMany(Message::class, 'user_id', 'to_user_id'); } + public function admin_chat_unread_messages() + { + return $this->hasMany(Message::class, 'to_user_id', 'to_user_id'); + } + public static function pin_chat(int $chat_id, $fixed) { return self::where('id', '=', $chat_id) @@ -79,8 +84,8 @@ class Chat extends Model { return Chat::query() ->with('last_message') - ->withCount(['unread_messages' => function ($query) use($user_id) { - $query->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); + ->withCount(['admin_chat_unread_messages' => function ($query) use($user_id) { + $query->whereNull('chat_id_from')->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); }]) ->where('to_user_id', '=', $user_id) ->where('is_admin_chat', 1) diff --git a/app/Models/MessagesRequests.php b/app/Models/MessagesRequests.php index b7dcb76..97ce560 100644 --- a/app/Models/MessagesRequests.php +++ b/app/Models/MessagesRequests.php @@ -33,18 +33,21 @@ class MessagesRequests extends Model $job_ids = json_decode($message_request->job_titles); if (!empty($job_ids)){ - $workers = Title_worker::select('worker_id') + $title_workers = Title_worker::select('worker_id') + ->with('worker') ->whereIN('job_title_id', $job_ids) ->groupBy('worker_id') ->get() ; - if ($workers->count()){ + + + if ($title_workers->count()){ $message_params = [ 'text' => $message_request->text ]; - foreach ($workers as $worker){ - Message::add_message(null, $message_request->user_id, $worker->worker_id, $message_params, file_store_path : '/', is_admin_chat: true); + foreach ($title_workers as $title_worker){ + Message::add_message(null, $message_request->user_id, $title_worker->worker->user_id, $message_params, file_store_path : '/', is_admin_chat: true); } } } diff --git a/app/Models/Title_worker.php b/app/Models/Title_worker.php index c984fe1..82b797f 100644 --- a/app/Models/Title_worker.php +++ b/app/Models/Title_worker.php @@ -8,4 +8,8 @@ use Illuminate\Database\Eloquent\Model; class Title_worker extends Model { use HasFactory; + + public function worker() { + return $this->belongsTo(Worker::class, 'worker_id'); + } } diff --git a/resources/views/chats/chats_list.blade.php b/resources/views/chats/chats_list.blade.php index f56e1a5..72cc9ad 100644 --- a/resources/views/chats/chats_list.blade.php +++ b/resources/views/chats/chats_list.blade.php @@ -17,6 +17,11 @@
@endif