Skip to content

Commit fb486f6

Browse files
author
Peter Klooster
committed
Fixes #15
1 parent 37869c1 commit fb486f6

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/VersioningScope.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class VersioningScope implements Scope
2525
*/
2626
public function apply(Builder $builder, Model $model)
2727
{
28-
$builder->join($model->getVersionTable(), function($join) use ($model) {
29-
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
30-
$join->on($model->getQualifiedVersionColumn(), '=', $model->getQualifiedLatestVersionColumn());
31-
});
28+
if (!$this->hasVersionJoin($builder, $model->getVersionTable())) {
29+
$builder->join($model->getVersionTable(), function($join) use ($model) {
30+
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
31+
$join->on($model->getQualifiedVersionColumn(), '=', $model->getQualifiedLatestVersionColumn());
32+
});
33+
}
3234

3335
$this->extend($builder);
3436
}
@@ -121,4 +123,15 @@ protected function isVersionJoinConstraint(JoinClause $join, $table)
121123
return $join->type == 'inner' && $join->table == $table;
122124
}
123125

126+
/**
127+
* Determine if the given builder contains a join with the given table
128+
*
129+
* @param Builder $builder
130+
* @param string $table
131+
* @return bool
132+
*/
133+
protected function hasVersionJoin(Builder $builder, string $table)
134+
{
135+
return collect($builder->getQuery()->joins)->pluck('table')->contains($table);
136+
}
124137
}

0 commit comments

Comments
 (0)