Laravel Order By Relationship Sum Column Example
https://www.itsolutionstuff.com/post/laravel-order-by-relationship-sum-column-exampleexample.html
Laravel Order By Relationship Sum Column Example
By Hardik Savani September 14, 2019 Category : LaravelPauseUnmuteLoaded: 2.17%FullscreenIf you are looking for how to laravel order by relation sum then i will help you how to create sum of eloquent relationships model and order by in laravel 6 and laravel 5 application. you can sum relationship column and orderby using subquery in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 project.
Laravel provide relationship concept and that's really very useful. If you use relationship in your laravel application then it make very easily stuff everything and you don't have to write long long database query. but some stuff like use sum, count etc function you can not use directly on relation model for order by. If you need order by on relation sum in laravel then i will give you bellow example that will help you.
In this bellow example, i created "customers" and "customer_balances" table and i manage each customer balance. I want to order by most highest balance customer should display on top order. So you can see bellow laravel db query for do this stuff with laravel 6 and laravel 5.
Laravel 6:
Laravel 5:
Output:
Read Also: Laravel 6 CRUD Application Tutorial
I hope it can help you.
Last updated