Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
How can i get the order of parents using number of child data in laravel

I want to get the order of Users table on the basis of roles.. for example if user has three roles its comes first, then user of two roles comes and so on her it is my query but it gives order to role table not parent table

$users = User::with(['roles'=>function($query){
        $query->orderBy('id','desc');
    }])->get();
    dd($users);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can use withCount method and order the results

$users =  User::query()
    ->withCount([
        'roles as no_of_roles_for_user'
    ])
    ->orderByDesc('no_of_roles_for_user')
    ->get();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!