app/Http/Controllers/ConversationsController.php
/**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
Display a listing of the resource.
@return \Illuminate\Http\Response
Display a listing of the resource . @return \ Illuminate \ Http \ Response
public function index()
    {
        $conversations = Conversation::where('sender_id', auth()->id())->orWhere('receiver_id', auth()->id())->get();
        $current_user = Auth::id();
        $users = [];
        $messages = [];
        foreach($conversations as $conv){
            $id = $conv["sender_id"] == $current_user ? $conv["receiver_id"] : $conv["sender_id"];
            array_push($users, User::find($id));
            array_push($messages, Message::where("conversation_id", $conv["id"])->orderBy('created_at', 'DESC')->take(1)->get()[0]);
        }
        return response()->json(["conversations" => $conversations, "users" => $users, "messages" => $messages], 200);
    }
public function index ( ) { $ conversations = Conversation :: where ( 'sender_id' , auth ( ) -> id ( ) ) -> orWhere ( 'receiver_id' , auth ( ) -> id ( ) ) -> get ( ) ; $ current_user = Auth :: id ( ) ; $ users = [ ] ; $ messages = [ ] ; foreach ( $ conversations as $ conv ) { $ id = $ conv [ " sender_id " ] == $ current_user ? $ conv [ " receiver_id " ] : $ conv [ " sender_id " ] ; array_push ( $ users , User :: find ( $ id ) ) ; array_push ( $ messages , Message :: where ( " conversation_id " , $ conv [ " id " ] ) -> orderBy ( 'created_at' , 'DESC' ) -> take ( 1 ) -> get ( ) [ 0 ] ) ; } return response ( ) -> json ( [ " conversations " => $ conversations , " users " => $ users , " messages " => $ messages ] , 200 ) ; }
Display a listing of the resource.
Display a listing of the resource .
// Check each conversation for user information. // Disregard authed user & push to users array. // Get Message Preview. Push most recent message to array.