Skip to content

Commit

Permalink
Fix eager loading with default model attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jul 20, 2022
1 parent 09af0a7 commit 9826c52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
17 changes: 17 additions & 0 deletions src/Relations/BelongsToJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public function getResults()
: $this->related->newCollection();
}

/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get($columns = ['*'])
{
$models = parent::get($columns);

if ($this->key && !empty($this->parent->{$this->path})) {
$this->hydratePivotRelation($models, $this->parent);
}

return $models;
}

/**
* Set the base constraints on the relation query.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Relations/HasManyJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public function getResults()
: $this->related->newCollection();
}

/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get($columns = ['*'])
{
$models = parent::get($columns);

if ($this->key && !is_null($this->parent->{$this->localKey})) {
$this->hydratePivotRelation($models, $this->parent);
}

return $models;
}

/**
* Set the base constraints on the relation query.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Relations/IsJsonRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@ public function __construct()
parent::__construct(...$args);
}

/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get($columns = ['*'])
{
$models = parent::get($columns);

if ($this->key && !empty($this->parent->getAttributes())) {
$this->hydratePivotRelation($models, $this->parent);
}

return $models;
}

/**
* Hydrate the pivot relationship on the models.
*
Expand Down

0 comments on commit 9826c52

Please sign in to comment.