Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Address.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Models\Common;
  3. use App\Concerns\Blamable;
  4. use App\Concerns\CompanyOwned;
  5. use App\Enums\Common\AddressType;
  6. use App\Models\Locale\Country;
  7. use App\Models\Locale\State;
  8. use Illuminate\Database\Eloquent\Casts\Attribute;
  9. use Illuminate\Database\Eloquent\Factories\HasFactory;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  12. use Illuminate\Database\Eloquent\Relations\HasMany;
  13. use Illuminate\Database\Eloquent\Relations\MorphTo;
  14. class Address extends Model
  15. {
  16. use Blamable;
  17. use CompanyOwned;
  18. use HasFactory;
  19. protected $table = 'addresses';
  20. protected $fillable = [
  21. 'company_id',
  22. 'parent_address_id',
  23. 'type',
  24. 'recipient',
  25. 'phone',
  26. 'address_line_1',
  27. 'address_line_2',
  28. 'city',
  29. 'state_id',
  30. 'postal_code',
  31. 'country',
  32. 'notes',
  33. 'created_by',
  34. 'updated_by',
  35. ];
  36. protected $casts = [
  37. 'type' => AddressType::class,
  38. ];
  39. public function addressable(): MorphTo
  40. {
  41. return $this->morphTo();
  42. }
  43. public function parentAddress(): BelongsTo
  44. {
  45. return $this->belongsTo(self::class, 'parent_address_id', 'id');
  46. }
  47. public function childAddresses(): HasMany
  48. {
  49. return $this->hasMany(self::class, 'parent_address_id', 'id');
  50. }
  51. public function country(): BelongsTo
  52. {
  53. return $this->belongsTo(Country::class, 'country', 'id');
  54. }
  55. public function state(): BelongsTo
  56. {
  57. return $this->belongsTo(State::class, 'state_id', 'id');
  58. }
  59. protected function addressString(): Attribute
  60. {
  61. return Attribute::get(function () {
  62. $street = array_filter([
  63. $this->address_line_1,
  64. $this->address_line_2,
  65. ]);
  66. return array_filter([
  67. implode(', ', $street), // Street 1 & 2 on same line if both exist
  68. implode(', ', array_filter([
  69. $this->city,
  70. $this->state->state_code,
  71. $this->postal_code,
  72. ])),
  73. ]);
  74. });
  75. }
  76. public function isIncomplete(): bool
  77. {
  78. return empty($this->address_line_1) || empty($this->city);
  79. }
  80. }