Laravel: How to order a Laravel hasMany relationship?
You can simply add a ->orderBy()
to the hasMany relationship
- return $this->hasMany(Post::class)->orderBy('updated_at');
You can simply add a ->orderBy()
to the hasMany relationship
- return $this->hasMany(Post::class)->orderBy('updated_at');
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
Blade Templating System
Controllers
Laravel Database Stuff
Eloquent
Routes and Routing
General Laravel Stuff
Laravel Collections
Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScriptOr see random questions
How do you run a PHP script from the command line?
How to hide certain file types from directory listings
How to pick what array_intersect function to use in PHP?
How to Extract Query String (from a URL) Into an Associative Array in PHP
How to set environmental variables in your .htaccess file
Point all requests to one PHP file
Force trailing slash (redirect if no trailing slash exists) in .htaccess
What are PHP's PSRs?
How do Invokable Controllers work in Laravel?
What is WordPress default Apache .htaccess rules?