how do laravel orderby string

Viewed 32

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();
0 Answers
Related