1

I use the following code to display the record

$q = '[20,1,5,40,3]';
$qex = explode(",",$q);
$list = DB::table('mytable')->whereIn('id', $qex)->get();

I want show the result like this :

result :20 1 5 40 3

but my reslut is :

1 3 5 20 40

I do not want the result to be sorted based on the id and I want it to be sorted based on the input I gave($q).

thanks

My problem solved by this code :

$list = DB::table('mytable')
  ->whereIn('id', $qex)
  ->orderByRaw(\DB::raw("FIELD(id, ".implode(",",$qex).")"))
  ->get();
  • Does this answer your question? [How to define a custom ORDER BY order in mySQL](https://stackoverflow.com/questions/9378613/how-to-define-a-custom-order-by-order-in-mysql) – nice_dev Apr 22 '22 at 07:46

0 Answers0