Skip to content

Commit c5324ef

Browse files
committed
handle whereIn when collection is empty
1 parent eeb78e0 commit c5324ef

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/CacheKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ protected function getInAndNotInClauses(array $where) : string
215215

216216
$type = strtolower($where["type"]);
217217
$subquery = $this->getValuesFromWhere($where);
218-
$values = collect($this->query->bindings["where"][$this->currentBinding]);
218+
$values = collect($this->query->bindings["where"][$this->currentBinding] ?? []);
219219
$this->currentBinding++;
220220
$subquery = collect(vsprintf(str_replace("?", "%s", $subquery), $values->toArray()));
221221
$values = $this->recursiveImplode($subquery->toArray(), "_");

tests/Integration/CachedBuilder/WhereInTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,30 @@ public function testWithInUsingCollectionQuery()
4141
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
4242
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
4343
}
44+
45+
46+
public function testWithInWhenSetIsEmpty()
47+
{
48+
$key = sha1('genealabs:laravel-model-caching:testing::memory::books:genealabslaravelmodelcachingtestsfixturesbook-author_id_in_1_2_3_4');
49+
$tags = [
50+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
51+
];
52+
$authors = (new UncachedAuthor)
53+
->where("id", "<", 0)
54+
->get(["id"]);
55+
56+
$books = (new Book)
57+
->whereIn("author_id", $authors)
58+
->get();
59+
$cachedResults = $this
60+
->cache()
61+
->tags($tags)
62+
->get($key)['value'];
63+
$liveResults = (new UncachedBook)
64+
->whereIn("author_id", $authors)
65+
->get();
66+
67+
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
68+
$this->assertNull($cachedResults);
69+
}
4470
}

0 commit comments

Comments
 (0)