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.