0

I am using laravel and want to sort this array based on ['messages'] "updated_at" field. The array should show latest message on top. I tried array_multisort( array_column($yourArray, "price"), SORT_ASC, $yourArray ) but didn't work.

Here is my multidimensional array which I want to sort by date.

Array
(
    [current_page] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 236
                    [user] => Array
                        (
                            [id] => 91
                            [email] => [email protected]
                            [updated_at] => 2020-04-29 09:28:26
                        )

                    [messages] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [id] => 235
                    [description] => demo3
                    [user] => Array
                        (
                            [id] => 91
                            [email] => [email protected]
                            [updated_at] => 2020-04-29 09:28:26
                        )
                    [messages] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 95
                                    [message] => Pickup message test
                                    [user_id] => 35
                                    [created_at] => 2020-04-28 12:28:18
                                    [updated_at] => 2020-04-28 12:28:18
                                )
                        )

                )

            [2] => Array
                (
                    [id] => 215
                    [description] => Testing for messages
                    [user] => Array
                        (
                            [id] => 35
                            [email] => [email protected]
                            [updated_at] => 2020-01-26 17:34:35
                        )

                    [messages] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 88
                                    [message] => Testing for the message if not sent by business first.
                                    [user_id] => 35
                                    [created_at] => 2020-04-28 12:15:14
                                    [updated_at] => 2020-04-28 12:15:14

                                )

                            [1] => Array
                                (
                                    [id] => 90
                                    [message] => Looks good.
                                    [user_id] => 69
                                    [created_at] => 2020-04-28 12:15:45
                                    [updated_at] => 2020-04-28 12:15:45

                                )


                        )

                )

        )
)

How can I achieve this?

1 Answer 1

2

Use usort function https://www.php.net/manual/en/function.usort.php

Write a callback comparing the nested created_at attribute

Also if you have a laravel collection you can use

$collection = $collection->sort(function ($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
});

Or if you are using eloquent you can order from SQL

Message::where(...)->orderBy('created_at', 'DESC')->paginate(20)

From a user relation

$user->with(['messages' => function ($query) {
    $query->orderBy('created_at', 'DESC');
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting data from this $claims = Claim::query(); $claims = $claims->where('status', $status); $claims->load('user', 'item', 'item.category', 'actions', 'actions.user', 'messages', 'messages.user'); how can I use this $user->with(['messages' => function ($query) { $query->orderBy('created_at', 'DESC'); }]);
do you need order only the messages or all collection?
I need to order all collection

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.