0

I am using Kyslik/column-sortable in my Laravel 5.4 CRUD project for added sortable column headers.

the model:

use Sortable;
// 
public $sortable = ['id', 'name', 'email', 'created_at', 'updated_at'];

the view:

<table class="table table-hover">
     <thead>
         <tr>
             <th> @sortablelink('id','#') </th>
             <th> @sortablelink('name') </th>
             ...
          </tr>
     </thead>
     <tbody>
     </tbody>
</table>

and the controller:

public function index(Request $request)
{
    $keyword = $request->get('search');
    //dd($request);
    if (!empty($keyword)) {
        $customers = Customer::where('name', 'LIKE', "%$keyword%")
            ->orWhere('email', 'LIKE', "%$keyword%")
            ->sortable()
            ->paginate(config('settings.perPage'));
    } else {
        $customers = Customer::paginate(config('settings.perPage'));
    }

    return view('admin.customers.index', compact('customers'));
}

The view and the column header links show up and work exactly as expected, but the results aren't sorted at all... What am I missing?

seekay
  • 2,493
  • 3
  • 27
  • 33
  • `->sortable()->orderBy('name','ASC')` http://stackoverflow.com/questions/17553181/laravel-4-how-to-order-by-using-eloquent-orm – Simon Müller Feb 26 '17 at 02:23
  • @SimonMüller from looking at the documentation, my understanding was that you needed to do that only if you wanted a default sort field+dir. Otherwise, it should pick it up directly from the request sent to the controller: https://github.com/Kyslik/column-sortable/blob/L5.4/src/ColumnSortable/Sortable.php#L24-L39 – seekay Feb 28 '17 at 00:55

1 Answers1

0

In your view page @sortablelink should have two arguments

  1. Column name in Database
  2. Column heading in table

So, @sortablelink('name','Name') This might help you.