1

So I want to "fix" the first three results in my array, that I get from my be so here is my example:

$ids = explode(',', '2,3,1');

$result = Items::whereIn('id', $ids)
                    ->where('active', '1')
                    ->get();

So I want this to return an array where the id 2 is the first, then 2, then 1. The order of the id's will be determent different each time, so I can't do something like order by id etc.

1 Answers1

2

Use Raw for FIELD

$result = Items::whereIn('id', $ids)
                    ->where('active', '1')
                    ->orderByRaw('FIELD(id,$ids)')
                    ->get();
Mihai
  • 26,325
  • 7
  • 66
  • 81