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 review blog posts (written by other users) before they get published on your WordPress blog?

How to Extract Query String (from a URL) Into an Associative Array in PHP

How to find out which php.ini file PHP is using?

How to replace values in a string, in JS

What is Bubble sort, and how to write an implementation of it in PHP

What is the difference between empty(), isset(), != null, is_null() in PHP

How to increment a value in the database

How to exclude URLs from the Laravel CSRF protection?

How to block an IP range based on CIDR in Apache with .htaccess

What are route patterns in Laravel?