問題描述
我已經(jīng)在我的 Laravel
應用程序中實現(xiàn)了 ZendSearch
.我使用它作為我的搜索引擎,用戶將在其中輸入一個搜索詞,然后 ZendSearch
將返回一個按相關性排序的結果數(shù)組.但是,ZendSearch
返回的數(shù)組只返回我的記錄 ID(它不返回任何實際的記錄信息).
I have implemented ZendSearch
into my Laravel
application. I am using it as my search engine where users will type a search word, and then ZendSearch
will return me an array of results ordered by relevance. However, the array that ZendSearch
returns, only returns my record ID's (it doesn't return any of the actual record information).
接下來查詢我的模型以檢索基于 ZendSearch
數(shù)組結果的結果的正確方法是什么,該數(shù)組結果只是一個基于相關性排序的 ID 數(shù)組.
What would next be the correct way to query my Model to retrieve the results based on the ZendSearch
array results which is just an array of ID's ordered based on relevance.
我知道 Model::find(1)
會返回我的 ID 為 1 的記錄,但是我如何為 find()
方法提供一個數(shù)組我希望按照我提供的順序返回的 ID.
I know of Model::find(1)
which would return my record with an ID of 1, but how can I feed that find()
method an array of ID's that I want to be returned in the order I am giving it.
推薦答案
這很簡單.使用 findMany
:
$models = Model::findMany([1, 2, 3]);
順便說一下,你也可以將一個數(shù)組傳遞給 find()
,它會在內部調用 findMany
:
By the way, you can also pass an array to find()
and it will internally call findMany
:
$models = Model::find([1, 2, 3]);
<小時>
在引擎蓋下它只是做了一個 whereIn
所以你也可以這樣做:
Under the hood it just does a whereIn
so you could do that too:
$models = Model::whereIn('id', [1, 2, 3])->get();
這篇關于基于多個 ID 檢索 Laravel 模型結果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!