Forked from sentiasa/laravel-query-too-many-placeholder.php
Created
March 6, 2023 01:48
-
-
Save allaniftrue/4f129e3a8219e58a7ff1099f87130bad to your computer and use it in GitHub Desktop.
Laravel - Query +100k placeholders in Laravel using `whereIn()`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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