Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save allaniftrue/4f129e3a8219e58a7ff1099f87130bad to your computer and use it in GitHub Desktop.
Save allaniftrue/4f129e3a8219e58a7ff1099f87130bad to your computer and use it in GitHub Desktop.
Laravel - Query +100k placeholders in Laravel using `whereIn()`
<?php
$transactionIds = Transaction::pluck('id'); // +100k transaction ids
$maxAtOneTime = 5000;
$total = count($transactionIds);
$pages = ceil($total / $maxAtOneTime);
$transactions = collect();
for ($i = 1; $i < ($pages + 1); $i++) {
$offset = (($i - 1) * $maxAtOneTime);
$start = ($offset == 0 ? 0 : ($offset + 1));
$data = Transaction::query()
->whereIn('id', $transactionIds)
->skip($start)
->take($maxAtOneTime)
->get();
$transactions = $transactions->merge($data);
}
// Now $transactions has all you need!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment