根据多个ID检索Laravel Model结果
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).
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.
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.
我已将 接下来是什么 查询我的模型以检索基于 我知道 Model :: find(1) code>,它将返回ID为1的记录,但是如何将 ZendSearch code>实现到我的
Laravel code>应用程序中。 我使用它作为我的搜索引擎,用户将在其中键入搜索词,然后
ZendSearch code>将返回一个按相关性排序的结果数组。 但是,
ZendSearch code>返回的数组只返回我的记录ID(它不返回任何实际记录信息)。 p>
ZendSearch code>数组结果的结果的正确方法,该结果只是基于相关性排序的ID数组。 p>
find() code>方法提供给我想要的ID数组 按照我给它的顺序返回。 p>
div>
That's simple. Use findMany
:
$models = Model::findMany([1, 2, 3]);
By the way, you can also pass an array to find()
and it will internally call findMany
:
$models = Model::find([1, 2, 3]);
Under the hood it just does a whereIn
so you could do that too:
$models = Model::whereIn('id', [1, 2, 3])->get();