Waqar Ali
Waqar Ali

Reputation: 43

How to orderBy with count function for a single colmn in Laravel?

I have a column which shows the number of total referrals a user have. Below is my code. I want to paginate order by the person have highest number of refs. I tried with max('totalref') but then paginate not works. So I am confused with this type of alignment

public function leaders()
{

    $accounts = Account::Where('totalref','>' , 5)->orderBy('id','ASC')->paginate(200);
    return view('admin.leaders', compact('accounts'));
}

Upvotes: 1

Views: 54

Answers (1)

mrhn
mrhn

Reputation: 18916

When you are working with paginated data, you want to do ordering, searching and etc. in the query. As based on your comment, it seems like totalref is a string in your database, convert it to an integer and the following will work.

$accounts = Account::Where('totalref','>' , 5)->orderBy('totalref','desc')->paginate(200);

Upvotes: 1

Related Questions