Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL orderByNullsLast doesn't work for strings and just turns order for integers #1822

Closed
mshd opened this issue Aug 19, 2018 · 2 comments · Fixed by #2191
Closed

MySQL orderByNullsLast doesn't work for strings and just turns order for integers #1822

mshd opened this issue Aug 19, 2018 · 2 comments · Fixed by #2191

Comments

@mshd
Copy link

mshd commented Aug 19, 2018

Summary of problem or feature request

I'm using orderByNullsLast with the config setting MySQL orderByNullsLast doesn't work for strings and for numbers and integers it will sort ascending like this 1,2,3,4,5,null,null,null, and descending like this null,null,nulll,5,4,3,2,1

Any idea idea how to fix that with MySQL?

Code snippet of problem

config/DataTables.php
'nulls_last_sql' => '-%s %s',

Controller.php

$return = Datatables::of(\DB::table($tablename));//nulls last
      foreach(...){
          $return = $return->orderColumn($name,$name."_int $1");
      }
      return $return->orderByNullsLast()->make(true);

-->

System details

  • Ubuntu 16.04
  • PHP 7.2
  • Laravel 5.6
  • MySQL
@mshd
Copy link
Author

mshd commented Aug 20, 2018

I found a solution, would be great if you can add it in /src/QueryDataTable.php

    protected function getNullsLastSql($column, $direction)
    {
        if(config('database.default') == "mysql"){
          $sql = 'IF(ISNULL(%s),1,0),%s %s';
          return sprintf($sql, $column, $column, $direction);
        }

@yajra
Copy link
Owner

yajra commented Aug 22, 2018

@mshd thanks! can you please submit a PR?

@github-actions github-actions bot locked and limited conversation to collaborators Oct 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants