diff --git a/src/EloquentDataTable.php b/src/EloquentDataTable.php index 700ac5bc..8c3f9ce8 100644 --- a/src/EloquentDataTable.php +++ b/src/EloquentDataTable.php @@ -138,6 +138,7 @@ protected function isNotEagerLoaded($relation) protected function joinEagerLoadedColumn($relation, $relationColumn) { $table = ''; + $deletedAt = false; $lastQuery = $this->query; foreach (explode('.', $relation) as $eachRelation) { $model = $lastQuery->getRelation($eachRelation); @@ -160,36 +161,48 @@ protected function joinEagerLoadedColumn($relation, $relationColumn) break; case $model instanceof HasOneOrMany: - $table = $model->getRelated()->getTable(); - $foreign = $model->getQualifiedForeignKeyName(); - $other = $model->getQualifiedParentKeyName(); + $table = $model->getRelated()->getTable(); + $foreign = $model->getQualifiedForeignKeyName(); + $other = $model->getQualifiedParentKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; case $model instanceof BelongsTo: - $table = $model->getRelated()->getTable(); - $foreign = $model->getQualifiedForeignKey(); - $other = $model->getQualifiedOwnerKeyName(); + $table = $model->getRelated()->getTable(); + $foreign = $model->getQualifiedForeignKey(); + $other = $model->getQualifiedOwnerKeyName(); + $deletedAt = $this->checkSoftDeletesOnModel($model->getRelated()); break; default: throw new Exception('Relation ' . get_class($model) . ' is not yet supported.'); } - $this->performJoin($table, $foreign, $other); + $this->performJoin($table, $foreign, $other, $deletedAt); $lastQuery = $model->getQuery(); } return $table . '.' . $relationColumn; } + protected function checkSoftDeletesOnModel($model) + { + if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) { + return $model->getDeletedAtColumn(); + } + + return false; + } + /** * Perform join query. * * @param string $table * @param string $foreign * @param string $other + * @param string $deletedAt * @param string $type */ - protected function performJoin($table, $foreign, $other, $type = 'left') + protected function performJoin($table, $foreign, $other, $deletedAt = false, $type = 'left') { $joins = []; foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) { @@ -199,5 +212,9 @@ protected function performJoin($table, $foreign, $other, $type = 'left') if (! in_array($table, $joins)) { $this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type); } + + if ($deletedAt !== false) { + $this->getBaseQueryBuilder()->whereNull($deletedAt); + } } }