I got a list of IDs and need to get a Collection (Let's say of the type "Book") and add Single Books to this Collection by Hand.

Example:

$ids = array(1,5,7,12);
$books = ???
foreach($ids as $id){
  $books->add(Book::find($id)); }

Or is there a easier way to get what I want?

Thank you

有帮助吗?

解决方案

You could write something like this I think:

$books = Book::whereIn('id', $ids)->get();

$books will be a Collection containing all your books.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top