0

I've been reading some documentations here and really not been able to find a solution on how to order the results in a specific order of a field name. NOT ALPHABETICALLY in a descending nor ascending order. Wanted to have this result:

$models = Model::orderByRaw('FIELD(name, "Student", "Beginner", "Professional")')->get();

How can I achieve this? Thank you.

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
user3233787
  • 379
  • 1
  • 10
  • 32

1 Answers1

0

Try this:

$models = Model::orderByRaw("FIELD(name , 'Student', 'Beginner', 'Professional') ASC")->get();
gerSu
  • 69
  • 8
  • Thanks for answering! Tried this but resulted to "bad sort specification" error. – user3233787 Oct 18 '21 at 11:57
  • Yes. I've already tried this beforehand and it's not working. I guess that this is due to the structure of mongodb's data that's why there's no sorting of fields in a specific order? – user3233787 Oct 18 '21 at 12:09
  • 1
    Apparently the jenssegers dosnt have support to the orderbyraw query, in the github there are some issues open that are talking about this. – gerSu Oct 18 '21 at 12:34
  • That is really unfortunate. Thanks. – user3233787 Oct 18 '21 at 12:39