2

I have an object which contains photo file paths,there caption and the order they apear in etc.

I need to sort the individual photos in order of the 'order', So that when i loop through each of the photos, they appear in the order specified by the order value.

Here is an example object:

    object: Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => Photo Object
                (
                    [timestamps] => 
                    [guarded:protected] => Array
                        (
                            [0] => id
                        )

                    [connection:protected] => 
                    [table:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 11
                            [res_id] => 1
                            [order] => 6
                            [owner_type] => Hotel
                            [owner_id] => 1
                            [gallery] => 
                            [caption] => 
                            [file] => Hotel_1_11.jpg
                            [deleted_at] => 
                        )

5
  • laravel.com/docs/5.1/eloquent search for orderBy Commented Jul 29, 2015 at 13:21
  • The issue is I can't query the database - I have to just manipulate the object printed above Commented Jul 29, 2015 at 13:26
  • Read the docs. It's not an issue. Commented Jul 29, 2015 at 13:27
  • In Laravel you can easily get them in order, with existing functions. Commented Jul 29, 2015 at 13:29
  • I found this $users = DB::table('users')->orderBy('name', 'desc')->get(); However this queries the database which I cant do. is there a way around?. I'm basically after what asort does to an array. Commented Jul 29, 2015 at 13:30

3 Answers 3

8

It's always better to sort data in the database, like that:

$photos = Photo::orderBy('order')->get();

This should give you better performance.

However, if above is not possible, you can call the following on your collection:

$collection = $collection->sortBy('order');

This will sort it in ascending order by the order field of whatever models you have there.

In case you want to sort them in descending order, do:

$collection = $collection->sortBy('order', SORT_REGULAR, true);
Sign up to request clarification or add additional context in comments.

Comments

2

Since Laravel 5.1 you can use a rather shorter syntax to order in descending order:

$collection = $collection->sortByDesc('order');

Instead of:

$collection = $collection->sortBy('order', SORT_REGULAR, true);

Comments

0

If you have a collection, and you want to sort it on basis of any of it's key, then you can use the following piece of code.

//if you want to sort in ascending order
$unique     = $collection->unique()->sortBy('YOUR_KEY_TO_SORT');

//if you want to sort in descending order
$unique     = $collection->unique()->sortBy('YOUR_KEY_TO_SORT',  SORT_REGULAR,  true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.