Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 867 Bytes

order-column.md

File metadata and controls

34 lines (24 loc) · 867 Bytes

Order Column

In some cases, you may want to use a custom order sql for a specific column. To achieve this, you can use orderColumn api.

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

In this example, we will order the column name with nulls as last result.

use DataTables;

Route::get('user-data', function() {
	$model = App\User::query();

	return DataTables::eloquent($model)
				->orderColumn('name', '-name $1')
				->toJson();
});

Here is another example of orderColumn using closure.

use DataTables;

Route::get('user-data', function () {
    $model = App\User::query();

    return DataTables::eloquent($model)
                 ->orderColumn('name', function ($query, $order) {
                     $query->orderBy('status', $order);
                 });
});