MySQL order by field in Eloquent
Using either DB::raw()
or orderByRaw
directly should work:
$models = Model::orderByRaw('FIELD(language, "USD", "EUR", "JPN")')->get();
// or
$models = Model::orderBy(DB::raw('FIELD(language, "USD", "EUR", "JPN")'))->get();
Use implode in the second parameter of FIELD()
$facilities = $query->with(['interest','city:id,name', 'state:id,name'])
->Active()
->whereIn('facility_id', $facilities_list)
->orderByRaw('FIELD(facility_id, '.implode(", " , $facilities_list).')')
->get();