app/Http/Controllers/ConversationController.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::all();
       return ConversationResource::collection($conversations) ;
       $conversations = Conversation::where('user_id',auth()->user()->id)->orWhere('second_user_id',auth()->user()->id)->orderBy('updated_at', 'desc')->get();
        $count = count($conversations);
        for($i=0 ; $i< $count ;$i++)
        {
            for($j=0 ; $j< $count ;$j++)
            {
                if($conversations[$i]->messages->last()->id < $conversations[$j]->messages->last()->id)
                {
                    $temp = $conversations[$i];
                    $conversations[$i]=$conversations[$j];
                    $conversations[$j]= $temp;
                }
            }
        }
       return ConversationResource::collection($conversations) ;
    }
public function index ( ) { $ conversations = Conversation :: all ( ) ; return ConversationResource :: collection ( $ conversations ) ; $ conversations = Conversation :: where ( 'user_id' , auth ( ) -> user ( ) -> id ) -> orWhere ( 'second_user_id' , auth ( ) -> user ( ) -> id ) -> orderBy ( 'updated_at' , 'desc' ) -> get ( ) ; $ count = count ( $ conversations ) ; for ( $ i = 0 ; $ i < $ count ; $ i ++ ) { for ( $ j = 0 ; $ j < $ count ; $ j ++ ) { if ( $ conversations [ $ i ] -> messages -> last ( ) -> id < $ conversations [ $ j ] -> messages -> last ( ) -> id ) { $ temp = $ conversations [ $ i ] ; $ conversations [ $ i ] = $ conversations [ $ j ] ; $ conversations [ $ j ] = $ temp ; } } } return ConversationResource :: collection ( $ conversations ) ; }
Display a listing of the resource.
Display a listing of the resource .
//        We use this to bring the messages ( select * from messages) //         The resouce is used to cover up a list of items in our case a list of all the conversations //         To create a resource : php artisan make:resource NameResource // we bring all conversations where the currently connected user is the  first or second user //        // we sort it by the last message sent