Skip to content

Commit 255e249

Browse files
authored
Merge pull request yajra#2191 from yajra/orderByNullsLast
[9.0] Improve orderByNullsLast SQL generation.
2 parents d8a965a + 95fae79 commit 255e249

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/QueryDataTable.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,11 @@ protected function getNullsLastSql($column, $direction)
690690
{
691691
$sql = $this->config->get('datatables.nulls_last_sql', '%s %s NULLS LAST');
692692

693-
return sprintf($sql, $column, $direction);
693+
return str_replace(
694+
[':column', ':direction'],
695+
[$column, $direction],
696+
sprintf($sql, $column, $direction)
697+
);
694698
}
695699

696700
/**

src/config/datatables.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
/*
55
* DataTables search options.
66
*/
7-
'search' => [
7+
'search' => [
88
/*
99
* Smart search will enclose search keyword with wildcard string "%keyword%".
1010
* SQL: column LIKE "%keyword%"
1111
*/
12-
'smart' => true,
12+
'smart' => true,
1313

1414
/*
1515
* Multi-term search will explode search keyword using spaces resulting into multiple term search.
1616
*/
17-
'multi_term' => true,
17+
'multi_term' => true,
1818

1919
/*
2020
* Case insensitive will search the keyword in lower case format.
@@ -26,48 +26,48 @@
2626
* Wild card will add "%" in between every characters of the keyword.
2727
* SQL: column LIKE "%k%e%y%w%o%r%d%"
2828
*/
29-
'use_wildcards' => false,
29+
'use_wildcards' => false,
3030

3131
/*
3232
* Perform a search which starts with the given keyword.
3333
* SQL: column LIKE "keyword%"
3434
*/
35-
'starts_with' => false,
35+
'starts_with' => false,
3636
],
3737

3838
/*
3939
* DataTables internal index id response column name.
4040
*/
41-
'index_column' => 'DT_RowIndex',
41+
'index_column' => 'DT_RowIndex',
4242

4343
/*
4444
* List of available builders for DataTables.
4545
* This is where you can register your custom dataTables builder.
4646
*/
47-
'engines' => [
48-
'eloquent' => \Yajra\DataTables\EloquentDataTable::class,
49-
'query' => \Yajra\DataTables\QueryDataTable::class,
50-
'collection' => \Yajra\DataTables\CollectionDataTable::class,
51-
'resource' => \Yajra\DataTables\ApiResourceDataTable::class,
47+
'engines' => [
48+
'eloquent' => Yajra\DataTables\EloquentDataTable::class,
49+
'query' => Yajra\DataTables\QueryDataTable::class,
50+
'collection' => Yajra\DataTables\CollectionDataTable::class,
51+
'resource' => Yajra\DataTables\ApiResourceDataTable::class,
5252
],
5353

5454
/*
5555
* DataTables accepted builder to engine mapping.
5656
* This is where you can override which engine a builder should use
5757
* Note, only change this if you know what you are doing!
5858
*/
59-
'builders' => [
59+
'builders' => [
6060
//Illuminate\Database\Eloquent\Relations\Relation::class => 'eloquent',
6161
//Illuminate\Database\Eloquent\Builder::class => 'eloquent',
6262
//Illuminate\Database\Query\Builder::class => 'query',
6363
//Illuminate\Support\Collection::class => 'collection',
6464
],
6565

6666
/*
67-
* Nulls last sql pattern for Posgresql & Oracle.
68-
* For MySQL, use '-%s %s'
67+
* Nulls last sql pattern for PostgreSQL & Oracle.
68+
* For MySQL, use 'CASE WHEN :column IS NULL THEN 1 ELSE 0 END, :column :direction'
6969
*/
70-
'nulls_last_sql' => '%s %s NULLS LAST',
70+
'nulls_last_sql' => ':column :direction NULLS LAST',
7171

7272
/*
7373
* User friendly message to be displayed on user if error occurs.
@@ -76,28 +76,28 @@
7676
* 'throw' - Throws a \Yajra\DataTables\Exceptions\Exception. Use your custom error handler if needed.
7777
* 'custom message' - Any friendly message to be displayed to the user. You can also use translation key.
7878
*/
79-
'error' => env('DATATABLES_ERROR', null),
79+
'error' => env('DATATABLES_ERROR', null),
8080

8181
/*
8282
* Default columns definition of dataTable utility functions.
8383
*/
84-
'columns' => [
84+
'columns' => [
8585
/*
8686
* List of columns hidden/removed on json response.
8787
*/
88-
'excess' => ['rn', 'row_num'],
88+
'excess' => ['rn', 'row_num'],
8989

9090
/*
9191
* List of columns to be escaped. If set to *, all columns are escape.
9292
* Note: You can set the value to empty array to disable XSS protection.
9393
*/
94-
'escape' => '*',
94+
'escape' => '*',
9595

9696
/*
9797
* List of columns that are allowed to display html content.
9898
* Note: Adding columns to list will make us available to XSS attacks.
9999
*/
100-
'raw' => ['action'],
100+
'raw' => ['action'],
101101

102102
/*
103103
* List of columns are are forbidden from being searched/sorted.
@@ -114,7 +114,7 @@
114114
/*
115115
* JsonResponse header and options config.
116116
*/
117-
'json' => [
117+
'json' => [
118118
'header' => [],
119119
'options' => 0,
120120
],

0 commit comments

Comments
 (0)