Skip to content

Commit

Permalink
make address as relation not property
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 8, 2023
1 parent 48ea245 commit 0aa5050
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Traits/AsCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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',
]);
}
}

0 comments on commit 0aa5050

Please sign in to comment.