Skip to content

Commit

Permalink
Auto delete addresses if the addressable is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
masterix21 committed Mar 6, 2024
1 parent 589da7f commit 3bec1fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Models/Concerns/HasAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Masterix21\Addressable\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Masterix21\Addressable\Concerns\UsesAddressableConfig;

trait HasAddresses
{
use UsesAddressableConfig;

public static function bootHasAddresses(): void
{
static::deleted(function (Model $deletedModel) {
$deletedModel->addresses()->delete();
});
}

public function addresses(): MorphMany
{
return $this->morphMany($this->addressModel(), 'addressable');
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Concerns/HasBillingAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Masterix21\Addressable\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Masterix21\Addressable\Concerns\UsesAddressableConfig;
Expand All @@ -10,6 +11,13 @@ trait HasBillingAddresses
{
use UsesAddressableConfig;

public static function bootHasBillingAddresses(): void
{
static::deleted(function (Model $deletedModel) {
$deletedModel->billingAddresses()->delete();
});
}

public function billingAddress(): MorphOne
{
return $this->morphOne($this->addressModel(), 'addressable')
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Concerns/HasShippingAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Masterix21\Addressable\Models\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Masterix21\Addressable\Concerns\UsesAddressableConfig;
Expand All @@ -10,6 +11,13 @@ trait HasShippingAddresses
{
use UsesAddressableConfig;

public static function bootHasShippingAddresses(): void
{
static::deleted(function (Model $deletedModel) {
$deletedModel->shippingAddresses()->delete();
});
}

public function shippingAddress(): MorphOne
{
return $this->morphOne($this->addressModel(), 'addressable')
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Concerns/ImplementsMarkPrimary.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait ImplementsMarkPrimary
{
use UsesAddressableConfig;

public function markPrimary()
public function markPrimary(): void
{
$this->is_primary = true;
$this->save();
Expand All @@ -37,7 +37,7 @@ public function markPrimary()
}
}

public function unmarkPrimary()
public function unmarkPrimary(): void
{
$this->is_primary = false;
$this->save();
Expand Down

0 comments on commit 3bec1fc

Please sign in to comment.