OrderBy case insensitive with Laravel

I am using Eloquent (Laravel 5.4) and a postgreSQL database. I created a table userswith a row column name. Executing this code returns users ordered by name, but the search is case sensitive:

$users = \App\User::orderBy('name')->get();

This code returns something like:

Alix
Beatrice
Zorro
adam
bill

Is there any way to get this case insensitive. I would like to have something like:

adam
Alix
Beatrice
bill
Zorro

Is there a way to get a "case insensitive order" from the result?

My question is about orderByEloquent, not sortBycollections.

+4
source share
2 answers

1. Why do you get this result?

, .

, () . Postgres , , "C" "POSIX". , App\User::where('name', 'aDaM')->first(); adam App\User::orderBy('name')->get(); . . Postgres en_US.UTF8 ( , UTF8).

2.

(1) ( Laravel)

App\User::orderBy('name')->get()->sortBy('name', SORT_NATURAL|SORT_FLAG_CASE);

Laravel 5.3, Eloquent get() .

(2) SQL-, ( Laravel),

DB::select('select name from users order by name COLLATE "en_US.utf8"');

.

(3) . .

dev Laravel Migrations and Seeders,

dev,

  • ( create)
  • ( p > )

, , , , , ( ) .

+4

SORT_NATURAL|SORT_FLAG_CASE orderBy, sortBy , orderBy , SQL-

0

Source: https://habr.com/ru/post/1680105/


All Articles