From cb51087219351c735cf852cec9d2e21fc80c1040 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Fri, 24 Nov 2023 13:49:31 +0100 Subject: [PATCH] Various improvements --- src/Relations/BelongsToThrough.php | 26 +++++++++++++------- src/Traits/BelongsToThrough.php | 38 ++++++++++++++++++++++-------- tests/Models/CustomerAddress.php | 2 +- 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/Relations/BelongsToThrough.php b/src/Relations/BelongsToThrough.php index abdc0eb..ab2b6db 100644 --- a/src/Relations/BelongsToThrough.php +++ b/src/Relations/BelongsToThrough.php @@ -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 */ @@ -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; @@ -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]; @@ -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))); diff --git a/src/Traits/BelongsToThrough.php b/src/Traits/BelongsToThrough.php index 27930c2..c2b7faf 100644 --- a/src/Traits/BelongsToThrough.php +++ b/src/Traits/BelongsToThrough.php @@ -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 = []; @@ -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 + ); } /** @@ -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 = []; @@ -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) @@ -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); } } diff --git a/tests/Models/CustomerAddress.php b/tests/Models/CustomerAddress.php index 2002d7f..f1f4825 100644 --- a/tests/Models/CustomerAddress.php +++ b/tests/Models/CustomerAddress.php @@ -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'],