Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Sep 22, 2019
1 parent b3cd944 commit aea93bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;

class BelongsToThrough extends Relation
Expand Down Expand Up @@ -128,7 +127,7 @@ protected function getForeignKeyName(Model $model)
*/
public function hasSoftDeletes(Model $model)
{
return in_array(SoftDeletes::class, class_uses_recursive(get_class($model)));
return in_array(SoftDeletes::class, class_uses_recursive($model));
}

/**
Expand Down Expand Up @@ -262,9 +261,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parent, $colu

$foreignKey = $parent->getQuery()->from.'.'.$this->getFirstForeignKeyName();

$foreignKey = new Expression($query->getQuery()->getGrammar()->wrap($foreignKey));

return $query->select($columns)->where(
return $query->select($columns)->whereColumn(
$this->getQualifiedFirstLocalKeyName(),
'=',
$foreignKey
Expand Down Expand Up @@ -316,4 +313,15 @@ public function getQualifiedFirstLocalKeyName()
{
return end($this->throughParents)->getQualifiedKeyName();
}

/**
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
protected function newRelatedInstanceFor(Model $parent)
{
return $this->related->newInstance();
}
}
3 changes: 1 addition & 2 deletions src/Traits/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ trait BelongsToThrough
*/
public function belongsToThrough($related, $through, $localKey = null, $prefix = '', $foreignKeyLookup = [])
{
/** @var \Illuminate\Database\Eloquent\Model $relatedInstance */
$relatedInstance = new $related;
$relatedInstance = $this->newRelatedInstance($related);
$throughParents = [];
$foreignKeys = [];

Expand Down

0 comments on commit aea93bd

Please sign in to comment.