diff --git a/src/Stache/Query/Builder.php b/src/Stache/Query/Builder.php index f1e89825c36..31294404634 100644 --- a/src/Stache/Query/Builder.php +++ b/src/Stache/Query/Builder.php @@ -157,19 +157,19 @@ protected function filterWhereBasic($values, $where) protected function filterWhereIn($values, $where) { - $lookup = array_flip($where['values']); + $lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values'])); return $values->filter( - fn ($value) => isset($lookup[$value]) + fn ($value) => isset($lookup[$value ?? '__NULL__']) ); } protected function filterWhereNotIn($values, $where) { - $lookup = array_flip($where['values']); + $lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values'])); return $values->filter( - fn ($value) => ! isset($lookup[$value]) + fn ($value) => ! isset($lookup[$value ?? '__NULL__']) ); } diff --git a/tests/Data/Entries/EntryQueryBuilderTest.php b/tests/Data/Entries/EntryQueryBuilderTest.php index 22950c866a2..8346a2a1695 100644 --- a/tests/Data/Entries/EntryQueryBuilderTest.php +++ b/tests/Data/Entries/EntryQueryBuilderTest.php @@ -91,6 +91,36 @@ public function entries_are_found_using_or_where_not_in() $this->assertEquals(['Post 3', 'Post 4'], $entries->map->title->all()); } + #[Test] + public function entries_are_found_using_where_in_with_null() + { + EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create(); + EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create(); + EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null + EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create(); + EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null + + $entries = Entry::query()->whereIn('category', ['news', null])->get(); + + $this->assertCount(4, $entries); + $this->assertEquals(['Post 1', 'Post 3', 'Post 4', 'Post 5'], $entries->map->title->all()); + } + + #[Test] + public function entries_are_found_using_where_not_in_with_null() + { + EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create(); + EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create(); + EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null + EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create(); + EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null + + $entries = Entry::query()->whereNotIn('category', ['news', null])->get(); + + $this->assertCount(1, $entries); + $this->assertEquals(['Post 2'], $entries->map->title->all()); + } + #[Test] public function entries_are_found_using_where_date() {