Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check SoftDeletes on HasOne or BelongsTo relations #1628

Merged
merged 4 commits into from
Mar 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/EloquentDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Copy link
Owner

@yajra yajra Mar 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this return the fully qualified column name? Can't check the codes yet but PR looks good. Thanks!

}

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')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put deletedAt as the last argument to prevent possible breaking change.

{
$joins = [];
foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) {
Expand All @@ -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);
}
}
}