Skip to content

Commit 13fcd1a

Browse files
authored
Merge pull request #325 from dmason30/master
Handle json contains with array value
2 parents 6cce090 + 91f92f2 commit 13fcd1a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/CacheKey.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ protected function getValuesFromWhere(array $where) : string
164164
return implode("_", collect($where["values"])->flatten()->toArray());
165165
}
166166

167+
if (is_array((new Arr)->get($where, "value"))) {
168+
return implode("_", collect($where["value"])->flatten()->toArray());
169+
}
170+
167171
return (new Arr)->get($where, "value", "");
168172
}
169173

tests/Integration/CachedBuilder/WhereJsonContainsTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,27 @@ public function testWithInUsingCollectionQuery()
4949
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
5050
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
5151
}
52+
53+
public function testWithInUsingCollectionQueryWithArrayValues()
54+
{
55+
$key = sha1("genealabs:laravel-model-caching:pgsql:testing:authors:genealabslaravelmodelcachingtestsfixturesauthor-finances->tags_jsoncontains_[\"foo\",\"bar\"]-authors.deleted_at_null");
56+
$tags = [
57+
'genealabs:laravel-model-caching:pgsql:testing:genealabslaravelmodelcachingtestsfixturesauthor',
58+
];
59+
60+
$authors = (new Author)
61+
->whereJsonContains("finances->tags", ['foo', 'bar'])
62+
->get();
63+
$liveResults = (new UncachedAuthor)
64+
->whereJsonContains("finances->tags", ['foo', 'bar'])
65+
->get();
66+
67+
$cachedResults = $this
68+
->cache()
69+
->tags($tags)
70+
->get($key)['value'];
71+
72+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
73+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
74+
}
5275
}

tests/database/factories/AuthorFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"total" => 5000,
1212
"weekly" => 100,
1313
"daily" => 20,
14+
'tags' => ['foo', 'bar'],
1415
],
1516
"is_famous" => $faker->boolean(),
1617
];

0 commit comments

Comments
 (0)