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

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Migration Durations and Job Memory Usage in Laravel 13.45 image

Migration Durations and Job Memory Usage in Laravel 13.45

Read article
Laravel CLI: One Binary for New PHP and Rust Laravel Apps image

Laravel CLI: One Binary for New PHP and Rust Laravel Apps

Read article
Phactorial: Render LaTeX Math to SVG and PNG in PHP image

Phactorial: Render LaTeX Math to SVG and PNG in PHP

Read article
Laravel, reimagined image

Laravel, reimagined

Read article
Laravel Rust: A New Rust Web Framework From Laravel image

Laravel Rust: A New Rust Web Framework From Laravel

Read article
Laravel Converse and Agent Kit for Chat Bots and AI Agents image

Laravel Converse and Agent Kit for Chat Bots and AI Agents

Read article