JulianNikolaz
JulianNikolaz

Reputation: 63

Laravel - Get specific rows first from db

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.

Upvotes: 1

Views: 678

Answers (1)

Mihai
Mihai

Reputation: 26784

Use Raw for FIELD

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

Upvotes: 2

Related Questions