根据多个ID检索Laravel模型结果 [英] Retrieve Laravel Model results based on multiple ID's

查看:121
本文介绍了根据多个ID检索Laravel模型结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Laravel应用程序中实现了ZendSearch.我将其用作我的搜索引擎,用户将在其中键入搜索词,然后ZendSearch将返回给我按相关性排序的结果数组.但是,ZendSearch返回的数组仅返回我的记录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数组结果检索结果的正确方法,该结果只是根据相关性对ID进行排序的数组.

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()方法提供我要按给出顺序返回的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]);

顺便说一句,您还可以将数组传递给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模型结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆