152

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
justinl
  • 10,448
  • 21
  • 70
  • 88
  • Care to comment why the downvote? – justinl Feb 14 '15 at 00:41
  • 1
    Another downvote? Why? :) The laravel docs don't even say anything about findMany() or the ability to pass an array to the find function. How is this not a legitimate question? :) – justinl Feb 21 '15 at 22:03
  • 4
    Up vote for you, this question did helps me. :) I didn't see `findMany` in the document either, and it's in the [API document](http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Builder.html#method_findMany). – Peter Liang Apr 27 '15 at 04:15
  • @PeterLiang broken link and I don't find on 8 – francisco Sep 23 '21 at 07:59

2 Answers2

335

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();
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
4

Just use ->find($ids)

$ids = [1,2,3,4]
$model = Model::find($ids);

in my case, i use query like this

$ids = [1,2,3,4]
$model = Model::query()->find($ids);

I used that in Lumen.