Laravel - How to Page and Give Order By

Asked

Viewed 1,141 times

1

I have that code:

$modeloVideo = ModeloVideo::paginate(10);

I want to page for 10 and display order by desc (descending order).

How to do this query?

I tried so :

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\ModeloVideo;

class HomeController extends Controller {

    public function home() {


        //TODOS OS REGISTROS DA TABELA
    $modeloVideo = ModeloVideo::all()->orderBy('created_at', 'desc')->paginate(10);

        $array = array("videos" => $modeloVideo,
        );

        return view('home',$array);
    }

Model code:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class ModeloVideo extends Model {
    //informando a tabela que sera usada
    protected $table = "modeloVideo";
    //quando fizer uma insercao ou update nao adicionar a coluna created ou update




}

?>

and it hasn’t worked yet, but the error: Badmethodcallexception Method orderby does not exist.

  • I have no way to test it now, but I believe with ModeloVideo::orderBy('created_at','desc')->paginate(10); should work, but you will receive one LengthAwarePaginator and will have to treat... use the dd(); to test. Which version of the Laravel you are using?

3 answers

2


To perform the search with the orderBy and the paginate just use the following syntax:

ModeloVideo::orderBy('created_at', 'desc')->paginate(10);

Just like this reply and that one in the Soen.

1

Avoid doing so, only a few cases:

$modeloVideo = ModeloVideo::all()->orderBy('created_at', 'desc')->paginate(10);

because, you’re bringing in the all() all records in the database in a Collection class and then there’s a mistake because it doesn’t exist orderby in class Collection the command is sortBy and finally generates a pagination, then is all wrong, because, already bring the mass of data (all records) from the base to then make ordering and pagination generates a big bottleneck in your application. A SQL built for this command is: SELECT * FROM modelo_video.

Correct way: first sorts values of the database and then makes the pagination all this already coming ready the data of your table, ie in SQL written by framework Eloquent, example:

$modeloVideo = ModeloVideo::orderBy('created_at', 'desc')->paginate(10);

and the SQL built for this command (basically speaking) is: SELECT * FROM modelo_video ORDER BY created_at DESC limit 10 offset 0, and this data already comes the specific quantity and this is the big difference. Observing: that one SQL is an example, the paginate method searches information for page in several of own Framework, That’s just to exemplify the neck part.

-2

before using the orderBy you need to search the records in the bank:

ModeloVideo::all()->orderBy('created_at', 'desc')->paginate(10);

->all(): search all bank records

->orderBy: orders the values sought

for paging you will need to use the method ->paginate, see the example in official documentation

The error is caused because the method orderBy is not part of instances of Model and yes of collections.

  • I couldn’t understand, can you enter the complete query that meets this condition please? $modeloVideo = Modelovideo::paginate(10); want to page by 10 and display order by desc

  • updated the Deny response

  • Thanks, but it still didn’t work, I updated the answer with the code.

  • publish which error you found...

  • Badmethodcallexception Method orderby does not exist.

  • 1

    thanks the holy will worked out just take the all()

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.