Reputation: 103
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.
Upvotes: 1
Views: 269
Reputation: 64466
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
Upvotes: 1