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

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


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

An overview of every main PHP array function

How to add a name prefix to all routes in a group?

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

How to force SSL in Apache's .htaccess?

How to see all of the options for an artisan command?

How do you access the php://input stream?

How to select all rows for a certain month (or day, year or time), using Eloquent

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

301 Redirect a single URL in .htaccess to a new location

What is the difference between reference and primitive data types types in Javascript?