2

I want to sort result from database by few colums. I trying like this:

Video::select('videoss.name as videoname', 'videos.*')->join('users', 'users.id', '=', 'videos.user_id')
        ->orderBy('videos.name', 'desc')
        ->orderBy('videos.views', 'desc')
        ->paginate(15);

but it is sorted only by name. How add sorting by views after sort by name.

Jensej
  • 1,255
  • 6
  • 21
  • 33
  • Do you have multiple videos in your database with the same name? – Jeff Lambert Oct 29 '15 at 16:47
  • it is possible, because user add this. – Jensej Oct 29 '15 at 17:00
  • I don't see an issue from the logic, the `orderBy` accepts multiple columns, I think it is data related. – Rabea Oct 29 '15 at 17:07
  • 1
    @user3458952 Unless you actually have multiple videos with the exact same name, you can't tell us whether or not the sorting isn't working on both fields as you want it to. – Jeff Lambert Oct 29 '15 at 17:11
  • @watcher does make a valid point. Please post some sample data from your table and what the result of your query is for that sample data. – Bogdan Oct 29 '15 at 17:34

1 Answers1

0

Have you tried in an array as explained in the comments here like this:

$user->orders = array(array('column' => 'name', 'direction' => 'desc'), array('column' => 'email', 'direction' => 'asc'));
Community
  • 1
  • 1
davejal
  • 6,009
  • 10
  • 39
  • 82