简体   繁体   中英

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.

Use Raw for FIELD

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM