app/Http/Controllers/MessagesController.php
/**
     * Poll request for new messages
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
Poll request for new messages
@param Request $request
@return \Illuminate\Http\JsonResponse
Poll request for new messages @param Request $request @return \ Illuminate \ Http \ JsonResponse
public function poll(Request $request)
    {
        $this->validate($request, [
            'chat_id' => 'required|numeric|exists:chats,id',
            'last_id' => 'required|numeric',
        ]);
        Chat::find($request->get('chat_id'))->see();
        $new_messages = Message::where($request->only(['chat_id']))
            ->where('id', '>', $request->get('last_id'))
            ->orderBy('created_at', 'desc')
            ->take(20)->get()->reverse();
        $data = [];
        foreach ($new_messages as $item) {
            $data[] = [
                'id'         => $item->id,
                'user_name'  => $item->user->full_name,
                'user_image' => $item->user->pictureUrl('thumb'),
                'user_link'  => url('profile/' . $item->user->id),
                'direction'  => $item->mine() ? 'sent' : 'received',
                'message'    => $item->message,
            ];
        }
        return response()->json($data);
    }
public function poll ( Request $ request ) { $ this -> validate ( $ request , [ 'chat_id' => 'required|numeric|exists:chats,id' , 'last_id' => 'required|numeric' , ] ) ; Chat :: find ( $ request -> get ( 'chat_id' ) ) -> see ( ) ; $ new_messages = Message :: where ( $ request -> only ( [ 'chat_id' ] ) ) -> where ( 'id' , '>' , $ request -> get ( 'last_id' ) ) -> orderBy ( 'created_at' , 'desc' ) -> take ( 20 ) -> get ( ) -> reverse ( ) ; $ data = [ ] ; foreach ( $ new_messages as $ item ) { $ data [ ] = [ 'id' => $ item -> id , 'user_name' => $ item -> user -> full_name , 'user_image' => $ item -> user -> pictureUrl ( 'thumb' ) , 'user_link' => url ( 'profile/' . $ item -> user -> id ) , 'direction' => $ item -> mine ( ) ? 'sent' : 'received' , 'message' => $ item -> message , ] ; } return response ( ) -> json ( $ data ) ; }
Poll request for new messages
 @param Request $request
 @return \Illuminate\Http\JsonResponse
Poll request for new messages @param Request $request @return \ Illuminate \ Http \ JsonResponse