Method orderBy does not exist in Laravel Eloquent?

just use the one line code it will work fine

$product= Product::orderBy('created_at','desc')->get();

If you want to get the list of all data and grab it in descending order try this:

$post = Post::orderBy('id', 'DESC')->get();

You're trying to use orderBy() method on Eloquent collection. Try to use sortByDesc() instead.

Alternatively, you could change $products = Product::all(); to $products = new Product();. Then all your code will work as you expect.


use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id