簡體   English   中英

Laravel-首先從數據庫獲取特定的行

[英]Laravel - Get specific rows first from db

所以我想“修復”我數組中的前三個結果,這是我的示例:

$ids = explode(',', '2,3,1');

$result = Items::whereIn('id', $ids)
                    ->where('active', '1')
                    ->get();

因此,我希望此方法返回一個數組,其中id為2的第一個,然后為2,然后為1。每次的id的順序都會不同,因此我無法執行諸如按id排序等操作。

將Raw用於FIELD

$result = Items::whereIn('id', $ids)
                    ->where('active', '1')
                    ->orderByRaw('FIELD(id,$ids)')
                    ->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM