Laravel

Make Laravel Queries Easier with whereIntegerInRaw()

Make Laravel Queries Easier with whereIntegerInRaw()

Filter large integer lists with less query-binding overhead.

  • 19 Sep, 2026
  • 29 Views

Make Laravel Queries Easier with whereIntegerInRaw()

Filter Large ID Lists with whereIntegerInRaw()

When filtering records using many IDs, you might normally write:

$users = User::whereIn('id', $userIds)->get();

This works well for most cases. However, a very large list of IDs can create significant query-binding overhead.

Laravel provides whereIntegerInRaw() as an alternative for integer lists.

Basic Usage

$users = User::whereIntegerInRaw('id', $userIds)->get();

Instead of binding every ID as a separate query parameter, Laravel inserts the integer values directly into the SQL query.

Real-World Example

Imagine you're generating a report for thousands of selected products:

$productIds = Product::query()
    ->where('active', true)
    ->pluck('id')
    ->all();

$products = Product::whereIntegerInRaw(
    'id',
    $productIds
)->get();

This can reduce the memory overhead associated with query bindings when the ID list is large.

Exclude Multiple IDs

Laravel also provides whereIntegerNotInRaw():

$products = Product::whereIntegerNotInRaw(
    'id',
    $excludedProductIds
)->get();

This retrieves products whose IDs are not in the supplied list.

When Should You Use It?

Consider this method when:

  • Filtering by thousands of integer IDs
  • Generating bulk reports
  • Processing large selections
  • Reducing query-binding memory overhead
Share: