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 force non www in htaccess

How to provide a default model object for relationships?

How to enable the rewrite engine in .htaccess?

Set PHP config Variables (like ini_set) with .htaccess

How to replace values in a string, in JS

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How to hide the server signature (hide Apache info) from HTTP headers with .htaccess

How to check if a user is logged in, inside a blade file?

How to find the index of a value in an array in JS?

How to block IPv6 IP addresses in .htaccess