Skip to content

Order/Sort parent based on Childrens #12

@Aniket-IN

Description

@Aniket-IN

In my application,
I have MessageThread and Message model.

This is the migration for message_threads table:

            $table->string('sender_number', 15);
            $table->string('receiver_number', 15);

            $table->string('sender_number_receiver_number', 30)->virtualAs('CONCAT(`sender_number`, "-", `receiver_number`)');
            $table->string('receiver_number', 30)->virtualAs('CONCAT(`receiver_number`, "-", `sender_number`)');

            $table->string('contact_name', 75)->nullable();
            $table->unique(['sender_number', 'receiver_number']);

And messages

            $table->string('from', 15);
            $table->string('to', 15);

            $table->string('from_to', 30)->virtualAs('CONCAT(`from`, "-", `to`)');
            $table->string('to_from', 30)->virtualAs('CONCAT(`to`, "-", `from`)');

            $table->text('text')->nullable();

With that, I have relations like this in MessageThread:

    public function messages(): HasManyMerged
    {
        return $this->hasManyMerged(Message::class, ['from_to', 'to_from'], 'sender_number_receiver_number');
    }

It works fine, I mean I can get all messages (incoming, outgoing) from a message thread.

But I want to get a list of Threads, based on Messages.
Like, Thread which has most recent message should be on top, and least recent thread to the bottom.

I'm sure it's not the best solution for messaging, seems too hacky, but this is what I could come up with.
Please suggest some way to sort the threads, or maybe suggest something different all together.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions