Order By on HasMany relationship Laravel 5

蓝咒 提交于 2021-01-28 22:06:02

问题


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


回答1:


Following should work.

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


来源:https://stackoverflow.com/questions/31366694/order-by-on-hasmany-relationship-laravel-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!