检索多个id的Laravel模态数据

问题描述:

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.

我有一个数组,我想根据数组值找到我的模态数据。我的数组看起来像 p >

  $ item = [5,3,4,1]; 
  code>  pre> 
 
 

我用于查找这些项目 p >

  $ topSell = Product :: find($ item); 
  code>  pre> 
 
 

它返回数据序列化,如1,3,4 5。 但我希望它将返回与我的数组序列化完全相同。 p> div>

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