1

I have an array,I want to find my modal data according to array value.My array looks like

$item = [5,3,4,1];

I used for find these items

$topSell = Product::find($item);

It returns data serialize like 1,3,4,5. But I want it will return exact same as my array serialization.

Masud Rana
  • 103
  • 12
  • 1
    Possible duplicate of [Laravel: How to get custom sorted eloquent collection using whereIn method](https://stackoverflow.com/questions/47270669/laravel-how-to-get-custom-sorted-eloquent-collection-using-wherein-method) – Chin Leung Dec 19 '17 at 11:15

1 Answers1

1

If you are using Mysql with laravel you can use Field() in order by

$topSell = Product::whereIn('id', $item)
    ->orderBy(DB::raw("FIELD(id,".join(',',$item).")"))
    ->get();

Field() returns the index position of a comma-delimited list

Reference

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118