0

I am fetching a list of data using Eloquent method and trying to reverse the order of array, I am getting an error.

Here is the code :(Previous Code)

$temp = Product::paginate(20);

New Code

$temp = Product::paginate(20)->orderBy('id','desc');

Error :

Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

New method :

$temp = Product::paginate(20)->sortDesc();

Error :

Method Illuminate\Database\Eloquent\Collection::appends does not exist. 

Can anyone help me?

3 Answers 3

2

use below syntax

Product::orderBy('id','desc')->paginate(20);
Sign up to request clarification or add additional context in comments.

1 Comment

Always explain your answer :-)
1

try to write it in this way, use orderBy before the paginate

$temp = Product::orderBy('id','desc')->paginate(20);

Comments

0

->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.

Product::latest()->paginate(20)

1 Comment

That is not the question, the question is to order the result with orderBy . latest() will work for an order on id, but since he is calling the functions in the wrong order, this is not the needed answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.