Laravel

How to retrieve a collection of models from an array of IDs in Laravel1 min read

To retrieve a collection of models from an array of IDs in Laravel, you can use the findMany method on the model’s query builder.

For example, if you have an array of user IDs and you want to retrieve a collection of User models, you could do the following:




This will retrieve all User models with an id that exists in the given array of IDs. The resulting collection will contain the retrieved models, which you can then manipulate or iterate over as needed.

You can also use the findMany method directly on the model’s query builder, which will return a collection of models keyed by their primary key:

This can be useful if you need to access the models by their primary key, rather than iterating over the collection.

Leave a Comment