phparrayslaravelsortinglaravel-4

Laravel sort array by created_at


I need to sort my json array in chronological order by it's created_at timestamps.

  [{
    "id": 1,
    "message": "hello",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 12:56:34",
    "updated_at": "2015-07-01 12:56:34"
  }, {
    "id": 2,
    "message": "helloWorld",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 12:56:53",
    "updated_at": "2015-07-01 12:56:53"
  }, {
    "id": 3,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 13:12:53",
    "updated_at": "2015-07-01 13:12:53"
  }, {
    "id": 4,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 13:13:04",
    "updated_at": "2015-07-01 13:13:04"
  }, {
    "id": 5,
    "message": "sup",
    "company_id": 7,
    "investor_id": 35,
    "match_id": 2,
    "created_at": "2015-07-01 15:06:39",
    "updated_at": "2015-07-01 15:06:39"
  }, {
    "id": 1,
    "message": "yo yo ",
    "investor_id": 35,
    "company_id": 7,
    "match_id": 2,
    "created_at": "2015-07-01 22:09:36",
    "updated_at": "-0001-11-30 00:00:00"
  }, {
    "id": 2,
    "message": "sup nigga",
    "investor_id": 35,
    "company_id": 7,
    "match_id": 2,
    "created_at": "2015-07-01 14:00:00",
    "updated_at": "-0001-11-30 00:00:00"
  }]

Can anyone teach me how do i have tried many solutions in stackoverflow . Many of them says array cannot be used with "sortBy" .

This is part of my code :

$companyMessage = Company_Message::where('match_id','=',$match_id);
    $investorMessage = Investor_Message::where('match_id','=',$match_id);
    $message = array();
    if($companyMessage->count()>0 || $investorMessage->count() > 0){
        if($lastActivityDate == null ){
            //load all messages before 
            if($companyMessage !=null){
                foreach ($companyMessage->get() as $cm) {
                    array_push($message,$cm);
                }
            }

            if($investorMessage !=null){
                foreach($investorMessage->get() as $im){
                    array_push($message,$im);
                }
            }

            return $message ;   

        }

Solution

  • I think you can do this using use a laravel database collection api instead of array and add item to collection and use sortBy method on collection,

    $col = new \Illuminate\Database\Eloquent\Collection();
    
    if ($companyMessage != null) {
      foreach($companyMessage - > get() as $cm) {
        $col->add($cm);
      }
    }
    if ($investorMessage != null) {
      foreach($investorMessage - > get() as $im) {
        $col->add($im);    
      }
    }
    
    $col = $col->sortBy('created_at');
    
    //return the json
    return Response::json($jsonArr->toArray());
    

    --- OR ----

    Try this but i didn't tested it. If this works then this would be the best way to do this.

    $companyMessage = Company_Message::where('match_id','=',$match_id);
    $investorMessage = Investor_Message::where('match_id','=',$match_id);
    
    //$companyMessage & $investorMessage both are laravel database collection,
    // so you can use merge method on collection to merge these two collections.
    
    $allResults = $companyMessage->merge($investorMessage);
    
    //use laravel collection sort method to sort the collection by created_at
    $allResults = $allResults->sortBy('created_at');
    
    //return the json
    return Response::json($allResults->toArray());