Skip to content

Commit

Permalink
Update zone relation on carrier, country and payment method Model (#265)
Browse files Browse the repository at this point in the history
* Update zone relation on carrier, country and payment method Model

* fix code formatting

---------

Co-authored-by: mckenziearts <[email protected]>
  • Loading branch information
mckenziearts and mckenziearts authored Jul 6, 2024
1 parent bf6a78d commit e600dda
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/Models/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Shopper\Core\Traits\HasSlug;
use Shopper\Core\Traits\HasZones;

/**
* @property-read int $id
Expand All @@ -25,6 +26,7 @@ class Carrier extends Model
{
use HasFactory;
use HasSlug;
use HasZones;

protected $fillable = [
'name',
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Shopper\Core\Traits\HasZones;

/**
* @property-read int $id
Expand All @@ -27,6 +27,7 @@
class Country extends Model
{
use HasFactory;
use HasZones;

public $timestamps = false;

Expand Down Expand Up @@ -64,9 +65,4 @@ public function svgFlag(): Attribute
fn () => url(shopper()->prefix() . '/images/flags/' . mb_strtolower($this->cca2) . '.svg')
);
}

public function zone(): MorphOne
{
return $this->morphOne(ZoneRelation::class, 'zonable');
}
}
13 changes: 7 additions & 6 deletions packages/core/src/Models/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Shopper\Core\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Shopper\Core\Traits\HasSlug;
use Shopper\Core\Traits\HasZones;

/**
* @property-read int $id
Expand All @@ -21,6 +23,7 @@ class PaymentMethod extends Model
{
use HasFactory;
use HasSlug;
use HasZones;

protected $guarded = [];

Expand All @@ -37,13 +40,11 @@ public function getTable(): string
return shopper_table('payment_methods');
}

public function getLogoUrlAttribute(): ?string
public function LogoUrl(): ?Attribute
{
if ($this->logo) {
return shopper_asset($this->logo);
}

return null;
return Attribute::make(
get: fn () => $this->logo ? shopper_asset($this->logo) : null,
);
}

public function scopeEnabled(Builder $query): Builder
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/Models/Zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @property \Shopper\Core\Models\Currency $currency
* @property \Illuminate\Database\Eloquent\Collection $carriers
* @property \Illuminate\Database\Eloquent\Collection $shippingOptions
* @property \Illuminate\Database\Eloquent\Collection $paymentMethods
*/
class Zone extends Model
{
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/Traits/HasZones.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Shopper\Core\Traits;

use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Shopper\Core\Models\Zone;

trait HasZones
{
public function zones(): MorphToMany
{
return $this->morphToMany(Zone::class, 'zonable', shopper_table('zone_has_relations'));
}
}

0 comments on commit e600dda

Please sign in to comment.