Donations Appreciated

When we give cheerfully and accept gratefully, everyone is blessed. - Maya Angelou

Click here to lend your support to: Laravel Datatables and make a donation at pledgie.com !

EloquentController.php - Order Column API

Order column has a special variable $1 which is being replace as the order direction value of the request.

->orderColumn('name', 'email $1')

In this demo, when name column is sorted, it will be sorted by email instead.

Id Name Email Created At Updated At

EloquentController.php

    public function getOrderColumn(Request $request)
    {
        if ($request->ajax()) {
            return Datatables::of(User::query())
                ->orderColumn('name', 'email $1')
                ->make(true);
        }

        return view('datatables.eloquent.order-column', ['title' => 'Order Column API']);
    }

Javascript

    $('#users-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: '',
        columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'created_at', name: 'created_at'},
            {data: 'updated_at', name: 'updated_at'}
        ]
    });