Skip to content

Commit c3b9bed

Browse files
authored
Merge pull request #2588 from PabloCamp0s/PabloCamp0s-patch-1
Fix morph relation support with multi level relations
2 parents 7e100ac + e8d96b6 commit c3b9bed

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/EloquentDataTable.php

Lines changed: 19 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,24 @@ 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+
$relationParts = explode('.', $relation);
136+
$firstRelation = array_shift($relationParts);
137+
$isMorph = $this->query->getModel()->$firstRelation() instanceof MorphTo;
138+
}
139+
140+
return $isMorph;
141+
}
142+
125143
/**
126144
* Check if a relation was not used on eager loading.
127145
*

0 commit comments

Comments
 (0)