diff --git a/src/CacheKey.php b/src/CacheKey.php index d5d2fad..9e3cd50 100644 --- a/src/CacheKey.php +++ b/src/CacheKey.php @@ -63,9 +63,14 @@ protected function getOrderByClauses() : string { $orders = collect($this->query->orders); - return $orders->reduce(function ($carry, $order) { - return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; - }) + return $orders + ->reduce(function ($carry, $order) { + if (($order['type'] ?? '') === 'Raw') { + return $carry . '_orderByRaw_' . str_slug($order['sql']); + } + + return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction']; + }) ?: ''; } diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index dbbe3bc..4fe23cb 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -639,4 +639,25 @@ public function testRelationshipQueriesAreCached() $this->assertTrue($cachedResults->diffAssoc($books)->isEmpty()); $this->assertTrue($liveResults->diffAssoc($books)->isEmpty()); } + + public function testRawOrderByWithoutColumnReference() + { + $authors = (new Author) + ->orderByRaw('DATE()') + ->get(); + + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_orderByRaw_date'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + + $liveResults = (new UncachedAuthor) + ->orderByRaw('DATE()') + ->get(); + + $this->assertTrue($cachedResults->diffAssoc($authors)->isEmpty()); + $this->assertTrue($liveResults->diffAssoc($authors)->isEmpty()); + } }