Laravel Tutorials

Precise Collection Filtering with Laravel's whereNotInStrict

Published
Precise Collection Filtering with Laravel's whereNotInStrict image

When you need exact type matching in your collection filtering operations, Laravel's whereNotInStrict method delivers precise results by ensuring both value and type must match during exclusion operations.

$collection = collect([
    ['id' => 1, 'value' => '100'],
    ['id' => 2, 'value' => 100],
    ['id' => 3, 'value' => '200'],
    ['id' => 4, 'value' => 200]
]);

$loose = $collection->whereNotIn('value', [100]);
// Result: Only items with value '200' and 200

$strict = $collection->whereNotInStrict('value', [100]);
// Result: Items with value '100', '200', and 200
// (keeps string '100' because 100 !== '100')

This distinction becomes particularly important in systems where data types carry meaningful differences, such as when working with identifiers or numeric values coming from different sources.

class ProductFilter
{
    public function filterByExactStock($products, array $excludedStockLevels)
    {
        return $products->whereNotInStrict('stock_level', $excludedStockLevels);
    }

    public function filterByPreciseStatus($products, array $statuses)
    {
        return $products
            ->whereNotInStrict('status', $statuses)
            ->values();
    }

    public function applyFilters($products)
    {
        return $products
            // Exclude specific numeric codes (type-sensitive)
            ->whereNotInStrict('product_code', ['001', '002'])
            // Exclude specific string statuses (type-sensitive)
            ->whereNotInStrict('status', ['active', 'pending'])
            // Re-index array
            ->values();
    }
}

$products = collect([
    ['product_code' => '001', 'status' => 'active'],
    ['product_code' => 1, 'status' => 'active'],    // Different type
    ['product_code' => '002', 'status' => 'pending'],
    ['product_code' => 2, 'status' => 'inactive']
]);

$filter = new ProductFilter();
$filtered = $filter->applyFilters($products);

Using whereNotInStrict allows you to maintain data integrity in your filtering operations by respecting the full identity of your values, preventing unintended matches when working with mixed data types or user inputs.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article
Real-Time Inertia Props With Laravel Reverb image

Real-Time Inertia Props With Laravel Reverb

Read article