latest()->take(50)->get()->reverse(); return view('chat.index', compact('messages')); } public function sendMessage(Request $request) { $request->validate([ 'message' => 'required|string', ]); $message = new Message(); $message->user_id = Auth::id(); $message->content = $request->input('message'); $message->save(); MessageSent::broadcast($message->content, $message->user_id, Auth::user()->name); return response()->json([ 'message' => $message->load('user'), ]); } }