From 0aa50501493bd5f3bf083e1bd888bffac245c22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Wed, 8 Nov 2023 14:09:37 +0100 Subject: [PATCH] make address as relation not property --- src/Traits/AsCustomer.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Traits/AsCustomer.php b/src/Traits/AsCustomer.php index d23da8fb..c58ca736 100644 --- a/src/Traits/AsCustomer.php +++ b/src/Traits/AsCustomer.php @@ -5,10 +5,10 @@ use Cone\Bazar\Models\Address; use Cone\Bazar\Models\Cart; use Cone\Bazar\Models\Order; -use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\MorphMany; +use Illuminate\Database\Eloquent\Relations\MorphOne; trait AsCustomer { @@ -45,14 +45,13 @@ public function addresses(): MorphMany } /** - * Get the address attribute. + * Get the default address for the user. */ - protected function address(): Attribute + public function address(): MorphOne { - return new Attribute( - get: function (): ?Address { - return $this->addresses->firstWhere('default', true) ?: $this->addresses->first(); - } - ); + return $this->addresses()->one()->ofMany([ + 'default' => 'max', + 'id' => 'min', + ]); } }