Masud Rana
Masud Rana

Reputation: 103

Retrieve Laravel Modal data for multiple id

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

Answers (1)

M Khalid Junaid
M Khalid Junaid

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

Reference

Upvotes: 1

Related Questions