R
R
Rishat Sultanov2017-08-22 13:43:48
Laravel
Rishat Sultanov, 2017-08-22 13:43:48

How to sort a collection by created_at?

Good afternoon.
There is this collection:

Collection {#476 ▼
  #items: array:19 [▼
    0 => GalleryAlbums {#477 ▼
      #table: "gallery_albums"
      #fillable: array:7 [▶]
      +timestamps: true
      -locale: "ru"
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:6 [▼
        "image" => ""
        "video_link" => "Kqk5nhWvpXA"
        "youtube_title" => "Поддержка малого и среднего бизнеса предприятиями"
        "id_gallery" => 6
        "id_gallery_group" => 48
        "created_at" => "2015-09-28 18:01:34"
      ]
      #original: array:6 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
    }
    1 => GalleryAlbums {#478 ▶}
    2 => GalleryAlbums {#479 ▶}
    3 => GalleryAlbums {#480 ▶}
    4 => GalleryAlbums {#481 ▶}
    5 => GalleryAlbums {#482 ▶}
    6 => GalleryAlbums {#483 ▶}
    7 => GalleryAlbums {#484 ▶}
    8 => GalleryAlbums {#485 ▶}
    9 => GalleryAlbums {#486 ▼
      #table: "gallery_albums"
      #fillable: array:7 [▶]
      +timestamps: true
      -locale: "ru"
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:6 [▼
        "image" => ""
        "video_link" => "szlsAtDt"
        "youtube_title" => "Крылатый металл"
        "id_gallery" => 6
        "id_gallery_group" => 48
        "created_at" => "2016-06-28 18:29:41"
      ]
      #original: array:6 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
    }
    10 => GalleryAlbums {#487 ▶}
    11 => GalleryAlbums {#488 ▶}
    12 => GalleryAlbums {#489 ▶}
    13 => GalleryAlbums {#490 ▶}
    14 => GalleryAlbums {#491 ▶}
    15 => GalleryAlbums {#492 ▶}
    16 => GalleryAlbums {#493 ▶}
    17 => GalleryAlbums {#494 ▶}
    18 => GalleryAlbums {#495 ▶}
  ]
}

How can it be sorted so that the elements in it are by the time they were added 2017 - 2016 - 2015 and so on. Now turn around.
I want to consider options without SQL queries. Since there is no way to do this for a number of reasons.
Buy orders will not pass gentlemen! Thank you )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2017-08-22
@rishatss

https://laravel.com/docs/5.4/eloquent-collections#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question