public function postRefresh(Request $request) { // check user auth and if has permission in chat room // If it is looking for new messages or old messages $new = $request->input('new'); // Get the latest post of the user $current_post_id = Chat::where('key', $request->input('msg_key'))->first()->id; // Get ID of that latest post from server $latest_post_id = Chat::orderBy('id', 'desc')->first()->id; // Preparation of response messages $response_posts = []; // Loop to fill the response array with proper data, to not include it all if ($latest_post_id > $current_post_id) { $latest_posts = Chat::where('id', '>', $current_post_id)->get(); foreach ($latest_posts as $post) { $response_posts[$post->id] = ['body' => $post->body, 'key' => $post->key, 'human_date' => $post->created_at->diffForHumans(), 'user' => ['name' => $post->user->username, 'avatar' => $post->user->getAvatar()]]; } } $json = ['data' => $response_posts]; return response()->json(json_encode($json)); // posts in JSON, that is then coded by JS }
public function index() { $chat = Chat::orderBy('created_at', 'asc')->get(); return view('home')->with('chat', $chat); }