Skip to content

Commit c858587

Browse files
authored
Merge pull request #232 from mycarrysun/fix-disable-cache
When model cache disabled, it is correctly disabled on query and all eager loaded relations
2 parents 023a585 + dfdd419 commit c858587

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/CachedBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ public function paginate(
166166
return $this->cachedValue(func_get_args(), $cacheKey);
167167
}
168168

169+
/**
170+
* Get the relation instance for the given relation name.
171+
* This is overloaded so we can disable model cache on
172+
* relations if parent has disabled model caching.
173+
*
174+
* @param string $name
175+
* @return \Illuminate\Database\Eloquent\Relations\Relation
176+
*/
177+
public function getRelation($name)
178+
{
179+
$relation = parent::getRelation($name);
180+
181+
if(!$this->isCachable() && is_a($relation->getQuery(), self::class)){
182+
$relation->getQuery()->disableModelCaching();
183+
}
184+
185+
return $relation;
186+
}
187+
169188
protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string
170189
{
171190
$result = "";

src/Traits/ModelCaching.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function destroy($ids)
6969
public function newEloquentBuilder($query)
7070
{
7171
if (! $this->isCachable()) {
72-
$this->isCachable = true;
72+
$this->isCachable = false;
7373

7474
return new EloquentBuilder($query);
7575
}

0 commit comments

Comments
 (0)