Skip to content

Commit 3056852

Browse files
authored
Merge pull request #247 from GeneaLabs/feature/add-belongs-to-many-cache
Add belongs-to-many caching functionality.
2 parents 17add5b + c40e37e commit 3056852

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+672
-896
lines changed

src/CacheKey.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class CacheKey
1919

2020
public function __construct(
2121
array $eagerLoad,
22-
Model $model,
23-
Builder $query
22+
$model,
23+
$query
2424
) {
2525
$this->eagerLoad = $eagerLoad;
2626
$this->model = $model;
@@ -54,7 +54,9 @@ protected function getIdColumn(string $idColumn) : string
5454

5555
protected function getLimitClause() : string
5656
{
57-
if (! $this->query->limit) {
57+
if (! property_exists($this->query, "limit")
58+
|| ! $this->query->limit
59+
) {
5860
return "";
5961
}
6062

@@ -74,7 +76,9 @@ protected function getModelSlug() : string
7476

7577
protected function getOffsetClause() : string
7678
{
77-
if (! $this->query->offset) {
79+
if (! property_exists($this->query, "offset")
80+
|| ! $this->query->offset
81+
) {
7882
return "";
7983
}
8084

@@ -83,6 +87,12 @@ protected function getOffsetClause() : string
8387

8488
protected function getOrderByClauses() : string
8589
{
90+
if (! property_exists($this->query, "orders")
91+
|| ! $this->query->orders
92+
) {
93+
return "";
94+
}
95+
8696
$orders = collect($this->query->orders);
8797

8898
return $orders
@@ -100,12 +110,15 @@ protected function getQueryColumns(array $columns) : string
100110
{
101111
if (($columns === ["*"]
102112
|| $columns === [])
103-
&& ! $this->query->columns
113+
&& (! property_exists($this->query, "columns")
114+
|| ! $this->query->columns)
104115
) {
105116
return "";
106117
}
107118

108-
if ($this->query->columns) {
119+
if (property_exists($this->query, "columns")
120+
&& $this->query->columns
121+
) {
109122
return "_" . implode("_", $this->query->columns);
110123
}
111124

@@ -302,7 +315,9 @@ protected function getWheres(array $wheres) : Collection
302315
{
303316
$wheres = collect($wheres);
304317

305-
if ($wheres->isEmpty()) {
318+
if ($wheres->isEmpty()
319+
&& property_exists($this->query, "wheres")
320+
) {
306321
$wheres = collect($this->query->wheres);
307322
}
308323

src/CacheTags.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class CacheTags
1616

1717
public function __construct(
1818
array $eagerLoad,
19-
Model $model,
20-
Builder $query
19+
$model,
20+
$query
2121
) {
2222
$this->eagerLoad = $eagerLoad;
2323
$this->model = $model;

src/CachedBelongsToMany.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching;
2+
3+
use Fico7489\Laravel\Pivot\Traits\FiresPivotEventsTrait;
4+
use GeneaLabs\LaravelModelCaching\Traits\Buildable;
5+
use GeneaLabs\LaravelModelCaching\Traits\BuilderCaching;
6+
use GeneaLabs\LaravelModelCaching\Traits\Caching;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
9+
class CachedBelongsToMany extends BelongsToMany
10+
{
11+
use Buildable;
12+
use BuilderCaching;
13+
use Caching;
14+
use FiresPivotEventsTrait;
15+
}

0 commit comments

Comments
 (0)