Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Antonella's avatar

query order text

I have a $ table-> text ('status') field that can take cat, dog, rabbit, t-rex (it's just these 4) I would like to query that orders for rabbit, dog, t-rex, cat?

0 likes
5 replies
Snapey's avatar

you use a where statement

write some code for review...

Antonella's avatar
Antonella
OP
Best Answer
Level 6

clearly it must not be a lexicographical order. IF IT WERE an ENUM type field I would order like this:

$exampleEnum = DB::table('polls')->orderByRaw('FIELD(status, "rabbit", "dog","t-rex","cat")asc')->orderBy('created_at', 'asc')->get();

BUT the field is TEXT TYPE

laracoft's avatar
DB::table('animal')->where('status', 'rabbit')->get(); // this will get all  the rabbits
Antonella's avatar

i noticed that orderByRaw also works on text

Please or to participate in this conversation.