Question: I would like to perform a sort by ascending or descending together with pagination

```
  return \App\User:: paginate($request->per_page);

```

how do i add the sort by so something like this

```
return \App\User:: paginate($request->per_page)-sortBy(
    $request->sort_field, $request->sort_order
  );

```

bur the above fails where 

```
$request->sort_order //asc or desc

```

the above returns an error of

```
"asort() expects parameter 2 to be integer, string given"

```

Answer: 
You could try something like 

```
User::orderBy($request->sort_field, $request->sort_order)->paginate($request->per_page);

```

Source: <https://laracasts.com/discuss/channels/laravel/combining-paginate-with-orderby>
I can't see the syntax in where you may enter the order (DESC or ASC) in the Laravel docs, Try this :

```
if ($request->sort_order == 'asc') {
   return \App\User::paginate($request->per_page)->sortBy($request->sort_field); 
}
return \App\User::paginate($request->per_page)->sortByDesc($request->sort_field); 

```

Read more in the docs : <https://laravel.com/docs/5.5/collections#method-sortby>