blamodex/laravel-addresses 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

blamodex/laravel-addresses

最新稳定版本:v1.0.0

Composer 安装命令:

composer require blamodex/laravel-addresses

包简介

Address management package for Laravel applications.

README 文档

README

License: MIT Laravel PHP Tests

A lightweight Laravel package to manage postal addresses, countries and administrative areas, suitable for attaching addresses to any Eloquent model using polymorphic relationships.

📋 Table of Contents

🚀 Features

  • Polymorphic Address model attachable to any Eloquent model via a trait
  • Lookup tables for Country and AdministrativeArea with seeders and migrations
  • Postal code normalization for US and Canada via (PostalCodeFormatter) with a clear, nullable contract
  • DB-backed AddressValidator to validate country, administrative area and postal codes
  • Service layer (AddressService) for create/update/delete/list operations
  • Soft deletes, UUID generation, and test coverage via Orchestra Testbench

📦 Installation

Install the package with Composer:

composer require blamodex/laravel-addresses

Publish the config file:

php artisan vendor:publish --tag=blamodex-address-config

Run the migrations:

php artisan migrate

⚙️ Configuration

Configuration lives in config/address.php (empty by default, but you can add options for formatting or validation):

return [
    // e.g. 'default_country' => 'CA'
];

🧩 Usage

1. Use the Addressable trait on models

For models that can have addresses:

use Blamodex\Address\Traits\Addressable;
use Blamodex\Address\Contracts\AddressableInterface;

class User extends Model implements AddressableInterface
{
    use Addressable;
}

2. Create an address

$user = User::find(1);

// Using the trait method
$address = $user->createAddress([
    'address_1' => '100 Main St',
    'city' => 'Anytown',
    'administrative_area_code' => 'CA',
    'postal_code' => '90001',
    'country_code' => 'US',
]);

// Or using the service directly
$service = app(\Blamodex\Address\Services\AddressService::class);
$address = $service->create($user, $attributes);

3. Update an address

$user->updateAddress($address, ['address_1' => '200 Main St']);

// Or via service
$service->update($address, ['address_1' => '200 Main St']);

4. Delete an address

$user->deleteAddress($address);

// Or via service
$service->delete($address);

5. Validation

Use the AddressValidator to check attributes before persisting:

$errors = \Blamodex\Address\Validators\AddressValidator::validate($attributes);
// Or throw on error
\Blamodex\Address\Validators\AddressValidator::validateOrFail($attributes);

PostalCodeFormatter::format() accepts ?string and returns ?stringnull indicates invalid or unsupported postal code.

🧪 Testing

This package uses Orchestra Testbench and PHPUnit.

Run tests:

composer test

Run tests with code coverage:

composer test:coverage

Check code style:

composer lint

Check code style and fix:

composer lint:fix

Check static analysis (phpstan) and tests as part of CI as configured in the repo.

📁 Project Structure

src/
├── Models/
│   ├── Address.php
│   ├── Country.php
│   └── AdministrativeArea.php
├── Services/
│   └── AddressService.php
├── Traits/
│   └── Addressable.php
├── Contracts/
│   └── AddressableInterface.php
├── Validators/
│   └── AddressValidator.php
├── Utils/
│   └── PostalCodeFormatter.php
├── config/
│   └── address.php
└── database/
    ├── migrations/
    └── seeders/

tests/
├── Unit/
│   ├── AddressServiceTest.php
│   ├── AddressTest.php
│   ├── AdministrativeAreaTest.php
│   ├── CountryTest.php
│   ├── PostalCodeFormatterTest.php
│   └── AddressValidatorTest.php
├── Fixtures/
│   ├── DummyAddressUser.php
│   └── DummyAddressCompany.php
└── TestCase.php

📄 License

MIT © Blamodex

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

🔗 Links

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-08