Virgil Cruz
Virgil Cruz

Reputation: 191

Laravel: Call to undefined method orderBy

I want to sort my list through descending order using "orderBy" in Laravel.

Im getting an error saying: Call to undefined method orderBy.

Here's my controller

 $sections = Section1::all()->orderBy('name', 'DESC')->get();

Upvotes: 1

Views: 6911

Answers (1)

DutGRIFF
DutGRIFF

Reputation: 5223

When you run Section1::all() you get the results as an object so you can't chain query builders such as orderBy. What you are looking for is:

 $sections = Section1::orderBy('name', 'DESC')->get();

Which says build a query for the Sections1 model ordered by name descending and get the results.

You can't use all() and get() together.

Upvotes: 1

Related Questions