Skip to content

Commit 4ca912a

Browse files
committed
Allow strings to be called by using Arr::wrap
1 parent 07a064a commit 4ca912a

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,14 @@ class MyCustomBuilder implements QueryCacheModuleInterface
374374
Since all of the Laravel Eloquent functions are based on it, the builder that comes with this package replaces only the `get()` one:
375375

376376
```php
377+
use Illuminate\Support\Arr;
378+
377379
class Builder
378380
{
379381
public function get($columns = ['*'])
380382
{
381383
if (! $this->shouldAvoidCache()) {
382-
return $this->getFromQueryCache('get', $columns);
384+
return $this->getFromQueryCache('get', Arr::wrap($columns));
383385
}
384386

385387
return parent::get($columns);
@@ -413,6 +415,8 @@ The default behaviour of the package doesn't use it, since the query builder use
413415
However, if your builder replaces functions like `find()`, `$id` is needed and you will also have to replace the `getQueryCacheCallback()` like so:
414416

415417
```php
418+
use Illuminate\Support\Arr;
419+
416420
class MyCustomBuilder
417421
{
418422
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null)
@@ -434,7 +438,7 @@ class MyCustomBuilder
434438
{
435439
// implementing the same logic
436440
if (! $this->shouldAvoidCache()) {
437-
return $this->getFromQueryCache('find', $columns, $id);
441+
return $this->getFromQueryCache('find', Arr::wrap($columns), $id);
438442
}
439443

440444
return parent::find($id, $columns);

src/Query/Builder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rennokki\QueryCache\Query;
44

55
use Illuminate\Database\Query\Builder as BaseBuilder;
6+
use Illuminate\Support\Arr;
67
use Rennokki\QueryCache\Contracts\QueryCacheModuleInterface;
78
use Rennokki\QueryCache\Traits\QueryCacheModule;
89

@@ -17,7 +18,7 @@ public function get($columns = ['*'])
1718
{
1819
return $this->shouldAvoidCache()
1920
? parent::get($columns)
20-
: $this->getFromQueryCache('get', $columns);
21+
: $this->getFromQueryCache('get', Arr::wrap($columns));
2122
}
2223

2324
/**

tests/GetTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ public function test_get_with_columns()
4444
$post->name
4545
);
4646
}
47+
48+
public function test_get_with_string_columns()
49+
{
50+
$post = factory(Post::class)->create();
51+
$storedPosts = Post::cacheFor(now()->addHours(1))->get('name');
52+
$cache = Cache::get('leqc:sqlitegetselect "name" from "posts"a:0:{}');
53+
54+
$this->assertNotNull($cache);
55+
56+
$this->assertEquals(
57+
$cache->first()->name,
58+
$storedPosts->first()->name
59+
);
60+
61+
$this->assertEquals(
62+
$cache->first()->name,
63+
$post->name
64+
);
65+
}
4766
}

0 commit comments

Comments
 (0)