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 add comments in PHP

How to exclude URLs from the Laravel CSRF protection?

How to block multiple IP addresses in .htaccess

How to check if a (blade) view file exists

How to reverse htmlentities() / What is the opposite of htmlentities()?

What does 'final class' and 'final function' (final method) mean in PHP?

What is the null coalescing operator in PHP7?

How to group a SQL statement in brackets/parenthesis

How to destroy or remove a cookie in PHP

How to return JSON data to the user in Laravel?