Bellots
Bellots

Reputation: 1773

Order By on HasMany relationship Laravel 5

In my Laravel 5 app, I've got a one-to-many relationship between two models.

My models are: User and Message.

I can access to messages from one user with:

$messages = $user->messages

How can I order these messages by creation date?

Thanks

Upvotes: 1

Views: 70

Answers (1)

Nicklas Kevin Frank
Nicklas Kevin Frank

Reputation: 6337

Following should work.

$messages = $user->messages()->orderBy('created_at')->get();

Upvotes: 2

Related Questions