28

What should I do to pass the values ​​that you order by in patinate?

routes.php

$ondata = DB::table('official_news') -> orderBy('created_date', 'desc') -> get() -> paginate(7);
return View::make('main',array('ondata' => $ondata));

error

Call to a member function paginate() on a non-object

I need your help

s0hno
  • 500
  • 1
  • 7
  • 12

3 Answers3

59

This should do the trick:

$ondata = DB::table('official_news')->orderBy('created_date', 'desc')->paginate(7);

  • 1
    @ErfanAhmedEmon yes it can even with relations. For example: Auth::user()->notifications()->orderBy('created_at', 'desc')->paginate(25); – Can Celik May 25 '16 at 03:03
30

It will be better if you order it by the id because it is going to be faster from the database.

$posts = Post::orderBy('id', 'desc')->paginate(6);

https://laracasts.com/discuss/channels/laravel/combining-paginate-with-orderby

It works fine for Laravel 5.1.

Steve Liddle
  • 3,635
  • 3
  • 26
  • 39
Santee
  • 418
  • 5
  • 12
2

You can use something like this in your controller file.

$transactions = Transaction::orderBy('name')->paginate(10);
return view('index', compact('transactions'));
Community
  • 1
  • 1