30

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
  • 520
  • 1
  • 7
  • 12

4 Answers4

62

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
33

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,685
  • 3
  • 27
  • 39
Santee
  • 448
  • 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
0

If you are using scope

 public function scopeSearch($query, $term)
    {
        $term = "%$term%";
        $query->where(function ($query) use ($term) {
            $query->where('name', 'like', $term)
                .............
                ..............
                ->orWhere('pan_number', 'like', $term);
        })->orderBy('created_at', 'DESC');
    }
Saugat
  • 1
  • 3