基于多个 ID 检索 Laravel 模型结果

Retrieve Laravel Model results based on multiple ID#39;s(基于多个 ID 检索 Laravel 模型结果)
本文介绍了基于多个 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 模型结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)