Skip to content

Commit 77c39f1

Browse files
authored
Fix morph relation support
Recent PR #2580 fails when a multiple level relationship is used ($relation string has several dot separated relations). I've created this fix in order to check only the first relation (I don't really know how Laravel behaves with multi level relationships when all or any are morphed).
1 parent 7e100ac commit 77c39f1

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/EloquentDataTable.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected function compileQuerySearch($query, $columnName, $keyword, $boolean =
9292
return parent::compileQuerySearch($query, $columnName, $keyword, $boolean);
9393
}
9494

95-
if ($this->query->getModel()->$relation() instanceof MorphTo) {
95+
if ($this->isMorphRelation($relation)) {
9696
$query->{$boolean . 'WhereHasMorph'}($relation, '*', function (Builder $query) use ($column, $keyword) {
9797
parent::compileQuerySearch($query, $column, $keyword, '');
9898
});
@@ -122,6 +122,25 @@ protected function resolveRelationColumn($column)
122122
return $this->joinEagerLoadedColumn($relation, $columnName);
123123
}
124124

125+
/**
126+
* Check if a relation is a morphed one or not.
127+
*
128+
* @param string $relation
129+
* @return bool
130+
*/
131+
protected function isMorphRelation($relation)
132+
{
133+
$isMorph = false;
134+
if ($relation !== null && $relation !== '')
135+
{
136+
$relationParts = explode('.', $relation);
137+
$firstRelation = array_shift($relationParts);
138+
$isMorph = $this->query->getModel()->$firstRelation() instanceof MorphTo;
139+
}
140+
141+
return $isMorph;
142+
}
143+
125144
/**
126145
* Check if a relation was not used on eager loading.
127146
*

0 commit comments

Comments
 (0)