How to order a Laravel hasMany relationship?

Laravel: How to order a Laravel hasMany relationship?

You can simply add a ->orderBy() to the hasMany relationship

  1. return $this->hasMany(Post::class)->orderBy('updated_at');

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to order a Laravel hasMany relationship?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to replace whitespace with a single space

How to force SSL in Apache's .htaccess?

How to redirect your website to a new domain/location in .htaccess

What is the difference between ( for ... in ) and ( for ... of ) in javascript?

How to namespace a Laravel route group?

How to order a Laravel hasMany relationship?

What are the SPL Iterators in PHP?

How to Enable Directory Listings in Apache with .htaccess

How to check if a string contains a certain substring?

How to prevent Eloquent from adding created_at or updated_at timestamps?