Skip to content

Commit

Permalink
add pivot loaded bool methods (#46555)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadoz authored Mar 28, 2023
1 parent 56dd3b1 commit 0fc5d30
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,36 @@ protected function whenPivotLoadedAs($accessor, $table, $value, $default = null)
}

return $this->when(
isset($this->resource->$accessor) &&
($this->resource->$accessor instanceof $table ||
$this->resource->$accessor->getTable() === $table),
$this->hasPivotLoadedAs($accessor, $table),
...[$value, $default]
);
}

/**
* Determine if the resource has the specified pivot table loaded.
*
* @param string $table
* @return bool
*/
protected function hasPivotLoaded($table)
{
return $this->hasPivotLoadedAs('pivot', $table);
}

/**
* Determine if the resource has the specified pivot table loaded with a custom accessor.
*
* @param string $accessor
* @param string $table
* @return bool
*/
protected function hasPivotLoadedAs($accessor, $table)
{
return isset($this->resource->$accessor) &&
($this->resource->$accessor instanceof $table ||
$this->resource->$accessor->getTable() === $table);
}

/**
* Transform the given value if it is present.
*
Expand Down

0 comments on commit 0fc5d30

Please sign in to comment.