Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Nov 24, 2023
1 parent 771c981 commit cb51087
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
26 changes: 17 additions & 9 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BelongsToThrough extends Relation
protected $foreignKeyLookup;

/**
* The custom foreign keys on the relationship.
* The custom local keys on the relationship.
*
* @var array
*/
Expand All @@ -62,8 +62,15 @@ class BelongsToThrough extends Relation
*
* @return void
*/
public function __construct(Builder $query, Model $parent, array $throughParents, $localKey = null, $prefix = '', array $foreignKeyLookup = [], array $localKeyLookup = [])
{
public function __construct(
Builder $query,
Model $parent,
array $throughParents,
$localKey = null,
$prefix = '',
array $foreignKeyLookup = [],
array $localKeyLookup = []
) {
$this->throughParents = $throughParents;
$this->prefix = $prefix;
$this->foreignKeyLookup = $foreignKeyLookup;
Expand Down Expand Up @@ -135,11 +142,12 @@ public function getForeignKeyName(Model $model = null)
}

/**
* Get the foreign key for a model.
* Get the local key for a model.
*
* @param \Illuminate\Database\Eloquent\Model|null $model
* @return string
*/
public function getLocalKeyName(Model $model = null)
public function getLocalKeyName(Model $model = null): string
{
$table = explode(' as ', ($model ?? $this->parent)->getTable())[0];

Expand Down Expand Up @@ -345,10 +353,10 @@ public function getFirstForeignKeyName()
}

/**
* Get the qualified local key for the first "through" parent model.
*
* @return string
*/
* Get the qualified local key for the first "through" parent model.
*
* @return string
*/
public function getQualifiedFirstLocalKeyName()
{
return end($this->throughParents)->qualifyColumn($this->getLocalKeyName(end($this->throughParents)));
Expand Down
38 changes: 28 additions & 10 deletions src/Traits/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ trait BelongsToThrough
* @param string|null $localKey
* @param string $prefix
* @param array $foreignKeyLookup
* @param mixed $localKeyLookup
*
* @param array $localKeyLookup
* @return \Znck\Eloquent\Relations\BelongsToThrough
*/
public function belongsToThrough($related, $through, $localKey = null, $prefix = '', $foreignKeyLookup = [], $localKeyLookup = [])
{
public function belongsToThrough(
$related,
$through,
$localKey = null,
$prefix = '',
$foreignKeyLookup = [],
array $localKeyLookup = []
) {
$relatedInstance = $this->newRelatedInstance($related);
$throughParents = [];
$foreignKeys = [];
Expand All @@ -48,7 +53,15 @@ public function belongsToThrough($related, $through, $localKey = null, $prefix =

$localKeys = $this->mapKeys($localKeyLookup);

return $this->newBelongsToThrough($relatedInstance->newQuery(), $this, $throughParents, $localKey, $prefix, $foreignKeys, $localKeys);
return $this->newBelongsToThrough(
$relatedInstance->newQuery(),
$this,
$throughParents,
$localKey,
$prefix,
$foreignKeys,
$localKeys
);
}

/**
Expand All @@ -57,7 +70,7 @@ public function belongsToThrough($related, $through, $localKey = null, $prefix =
* @param array $keyLookup
* @return array
*/
protected function mapKeys(array $keyLookup)
protected function mapKeys(array $keyLookup): array
{
$keys = [];

Expand All @@ -77,7 +90,6 @@ protected function mapKeys(array $keyLookup)
* Create a through parent instance for a belongs-to-through relationship.
*
* @param string $model
*
* @return \Illuminate\Database\Eloquent\Model
*/
protected function belongsToThroughParentInstance($model)
Expand All @@ -104,11 +116,17 @@ protected function belongsToThroughParentInstance($model)
* @param string $prefix
* @param array $foreignKeyLookup
* @param array $localKeyLookup
*
* @return \Znck\Eloquent\Relations\BelongsToThrough
*/
protected function newBelongsToThrough(Builder $query, Model $parent, array $throughParents, $localKey, $prefix, array $foreignKeyLookup, array $localKeyLookup)
{
protected function newBelongsToThrough(
Builder $query,
Model $parent,
array $throughParents,
$localKey,
$prefix,
array $foreignKeyLookup,
array $localKeyLookup
) {
return new Relation($query, $parent, $throughParents, $localKey, $prefix, $foreignKeyLookup, $localKeyLookup);
}
}
2 changes: 1 addition & 1 deletion tests/Models/CustomerAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CustomerAddress extends Model
public function vendorCustomer(): BelongsToThrough
{
return $this->belongsToThrough(
VendorCustomer::class,
VendorCustomer::class,
VendorCustomerAddress::class,
foreignKeyLookup: [VendorCustomerAddress::class => 'id'],
localKeyLookup: [VendorCustomerAddress::class => 'customer_address_id'],
Expand Down

0 comments on commit cb51087

Please sign in to comment.