Sort by rows and limit the result in Laravel 5

I am trying to get the result from a database in a laravel 5 based application, and for me life cannot understand.

I want to select the top 5 DESC results from a row called count. This is what I have:

$full = Fulls::all()->orderBy('count', 'desc')->take(5)->get(); 

I also tried a lot, but nothing works. Now I get the error message:

FatalErrorException on line indexController.php 19: calling the undefined method Illuminate \ Database \ Eloquent \ Collection :: orderBy ()

However, wherever I look, I see people working with orderBy() , so ... what am I doing wrong?

Thanks in advance...

+6
source share
2 answers

Instead, use Fulls::orderBy(..)->take(5)->get() .

+12
source

If you want to sort / organize the collection, you can use the sortBy () method.

eg.

 $full = Fulls::get(); // Get the Fulls collections $full = $full->sortBy('count')->take(5); 
+1
source

Source: https://habr.com/ru/post/984265/


All Articles