Search code examples
databaselaravelsql-order-by

Laravel order by desc


I'm trying to get this order by descend working, but for some reason an error keeps being thrown which is:

BadMethodCallException in View.php line 387: Method [orderBy] does not exist on view.

I'm not sure why it can't find the orderBy method.

public function display()
{
    return view('users/timeline')
        ->with('user_name', 'body', 'location',
            'photo', 'visibility', 'created_at')
        ->with('posts', Posts::all());
        ->orderBy('created_at', 'desc')
        ->get();
}

Solution

  • It should be:

    Posts::orderBy('created_at', 'desc')->get();