Skip to content

Commit

Permalink
Ignore Pivot models
Browse files Browse the repository at this point in the history
  • Loading branch information
anorgan authored Oct 11, 2017
1 parent fb8166c commit a80d018
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/TagFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Anorgan\LaravelCache;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Collection;

/**
Expand All @@ -18,6 +19,12 @@ class TagFinder
public function find($models)
{
if ($models instanceof Model) {

// Ignore Pivot models
if ($models instanceof Pivot) {
return [];
}

$models = collect([$models]);
}

Expand Down Expand Up @@ -45,13 +52,21 @@ private function getTagsFromCollection(Collection $collection)
*/
private function getTags(Model $model)
{
if ($model instanceof Pivot) {
// Ignore Pivot models
return [];
}

$tags = [
$this->createTag($model),
];

foreach ($model->getRelations() as $relation) {
if ($relation instanceof Collection) {
$tags = array_merge($tags, $this->getTagsFromCollection($relation));
} elseif ($relation instanceof Pivot) {
// Ignore Pivot models
$tags = [];
} elseif ($relation instanceof Model) {
$tags = array_merge($tags, $this->getTags($relation));
}
Expand Down

0 comments on commit a80d018

Please sign in to comment.