soatdev/addresses

此包通过使用表格和特质为类添加地址和地理位置。

dev-master 2020-05-17 23:01 UTC

This package is auto-updated.

Last update: 2024-09-18 08:01:24 UTC


README

Total Downloads Latest Stable Version Latest Unstable Version License

Laravel地址管理器是一个Laravel的扩展包,它通过自定义数据库表和特质为任何类提供地址管理和地理位置功能。它可以用于向任何类添加一个或多个地址,并附带预构建的表单组件,以便用户管理地址。

这是我第一个包,它还需要很多工作,但它是一个开始。

-- 请注意,此包仅适用于Laravel 5,尚未在旧版本上进行测试

安装

soatdev/addresses 添加到 composer.json

"soatdev/addresses": "dev-master"

运行 composer update 以拉取最新版本。

编辑 app/config/app.php 并添加 providerfacade

'providers' => [
    //Collective
    Collective\Html\HtmlServiceProvider::class,
    //Soatdev
    \Soatdev\Addresses\AddressesServiceProvider::class,
]

现在添加别名。

'aliases' => [
    //Collective
    'Form' => Collective\Html\FormFacade::class,
    'Html' => Collective\Html\HtmlFacade::class,

    //Soatdev
    'Addresses' => Soatdev\Addresses\Facades\Addresses::class,
]

初始化

您可以从发布配置开始。这是必需的步骤,它包含使用包所需的迁移、数据填充、视图、控制器和特质

$ php artisan vendor:publish

然后迁移数据库

$ php artisan migrate

为确保数据已填充,在 seeds/DatabaseSeeder.php 中插入以下代码

//Seed the countries
$this->call(SeedCountriesTable::class);
//Seed the street types
$this->call(SeedStreetTypesTable::class);

然后运行

composer dump-autoload

并填充数据库

$ php artisan db:seed

对于地区和城市,您可以使用sql文件,我将在以后添加填充器。待办事项:为地区和城市创建填充器

用法

现在,您可以将 Geolocalizable特质 添加到您想要添加地址的任何类中

use \Soatdev\Addresses\Traits\Geolocalizable;

并使用表单宏来显示地址表单字段。您需要在这个宏周围包装一个表单,该表单执行您想要的任何操作

{!! Form::sd_address_fields($errors) !!}

然后,在处理表单操作的控制器中,您可以调用Address的静态store方法,传入请求以及您要绑定地址的对象(该对象需要使用Geolocalizable特质

    //Create address for a geolocalizable entity
    $result = Address::store($request, $entity);
    if(!($result instanceOf Address)){
        //If the store method fails, it returns the validator tha can then be sent back with a redirect.
        return back()
        ->withErrors($result)
        ->withInput();
    }
    //If the store method succeeds, it return the Address instance created for the entity.
    return $entity->addresses();

配置

待办事项

测试

发布此包时,它会复制运行测试所需的所有必要工厂

vendor/bin/phpunit vendor/soatdev/addresses/tests

感谢

特别感谢Christoph Kempen和Paul Kievits的laravel-countries包,以及Rohit Kumar对国家、地区和城市数据库的数据库集成:hiiamrohit/Countries-States-Cities-database