public function update($id) { try { $thread = Thread::findOrFail($id); } catch (ModelNotFoundException $e) { Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.'); return redirect('messages'); } $message = Message::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'body' => Input::get('message')]); $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => Auth::user()->id]); $participant->last_read = new Carbon(); $participant->save(); return redirect('/message/' . $id); }
public function addParticipants($participants, $id) { Participant::firstOrCreate(['user_id' => $participants, 'thread_id' => $id]); }
public function updateMessage(Request $request, $slug) { $this->validate($request, ['body' => 'required']); $update = Thread::where('slug', $slug)->first(); $participant = Participant::where('thread_id', $update->id)->where('user_id', $request->user()->id)->first(); if ($update && $participant) { $update->activateAllParticipants(); $message = new Message(); $message->thread_id = $update->id; $message->user_id = $request->user()->id; $message->body = $request->get('body'); $message->save(); $participant = Participant::firstOrCreate(['thread_id' => $update->id, 'user_id' => $request->user()->id]); $participant->last_read = new Carbon(); $participant->save(); return redirect()->back()->withMessage('Сообщение отправлено.'); } else { return redirect()->route('messages')->withError('Ошибка, свяжитесь с администратором.'); } }