Method orderBy does not exist - laravel

How can i use the orderBy method with this script:

 public function getProposalByUser($userId)
{
    return ProposalModel::where('user_id', '=', $userId)->paginate(4)->orderBy('desc');
}

the script is to select the proposal where user have. and i want to arrange it for the top is newest. Thanks in advance, and sorry for my bad english..

Upvotes: 0

Views: 2115

Answers (3)

Don't Panic
Don't Panic

Reputation: 41820

It seems the main problem is that you have used the methods in the wrong order.

public function getProposalByUser($userId)
{
    return ProposalModel::where('user_id', '=', $userId)->orderBy('desc')->paginate(4);
}

paginate actually returns the query results in a collection, and the collection doesn't have an orderBy method, so you can't chain it like you can with the other query builder methods.

Secondly, it looks like the orderBy method needs another argument, unless your entity actually does have a property called 'desc'.

Upvotes: 3

Try this:

return ProposalModel::where('user_id', '=', $userId)->orderBy('created_date', 'desc')->paginate(4);

Upvotes: 0

José A. Zapata
José A. Zapata

Reputation: 1297

Try this:

return ProposalModel::where('user_id', '=', $userId)->orderBy('user_id', 'desc')->paginate(4);

Upvotes: 0

Related Questions