P
P
Paltos2018-10-12 03:38:22
Laravel
Paltos, 2018-10-12 03:38:22

What is the best way to sort entries alphabetically?

I use this code and it works great.
But a bit not as I need (

public function onRun ()
{
    $this->authors = $this->loadAuthors();
}

protected function loadAuthors()
{
    $query = AppAuthor::all();

    if ($this->property('sortOrder') == 'name asc') {
        $query = $query->sortBy('name');
    }

    if ($this->property('sortOrder') == 'name desc') {
        $query = $query->sortByDesc('name');
    }

    if ($this->property('results') > 0) {
        $query = $query->take($this->property('results'));
    }

    return $query;
}

The result is
Anton
Borya
Valik
Gena
Denya
But I need something to sort first by Cyrillic, then by Latin, numbers and signs:
Like this:
Gena
Denya
Anton
Borya
Valik
01killer
777Vasya
<3LoveIs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Paltos, 2018-10-12
@Paltinik

Did it like this:

protected function loadAuthors(){
        $table = New AppAuthor;
        $table = $table->table;
        $query = DB::select("
          select *
          from {$table} 
          ORDER BY 
          name < 'а', //русская
          name < 'a', //английская
          name < '0',
          name");



      if($this->property('results') > 0) {
       $query = $query->take($this->property('results'));
      }

      return $query;

    }

It seems to be working norms) I wanted to get data through the model, and not manually prescribe the query. the model has related tables, to get data from them you will have to manually write queries a little..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question