hichxm/laravel-phones

Hichxm 开发的用于 Laravel 生态系统的可排序包

v1.1 2024-04-01 23:40 UTC

This package is auto-updated.

Last update: 2024-10-02 01:19:29 UTC


README

Laravel Phones 是一个提供简单方式来管理与 Laravel 应用程序中的模型相关的电话号码的包。

安装

您可以通过 composer 安装此包

composer require hichxm/laravel-phones

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Hichxm\LaravelPhones\LaravelPhonesServiceProvider" --tag="config"

您需要使用以下命令发布和运行迁移

php artisan vendor:publish --provider="Hichxm\LaravelPhones\LaravelPhonesServiceProvider" --tag="migrations"

迁移发布后,您可以运行迁移来创建电话号码表

php artisan migrate

使用方法

首先,将 HasPhones 特性添加到您想要关联电话号码的模型中。

use Hichxm\LaravelPhones\Traits\HasPhones;

class User extends Model
{
    use HasPhones;
}

然后,您可以使用 phones 关系来管理电话号码。

$user = User::find(1);

$user->phones()->create([
    'number' => '1234567890',
    'type' => 'mobile',
]);

$user->addPhone('+33987654321');
$user->addPhone('0987654321', ['FR']); // with country code, will be stored as +33987654321
$user->addPhone('+33612345678', [], 'mobile'); // with type

您也可以使用 phones 关系来检索电话号码。

$user = User::find(1);

$phones = $user->phones;
$mobilePhones = $user->phones()->where('type', 'mobile')->get();

// Use scopes
$phones = $user->getPhones();
$mobilePhones = $user->getPhones('mobile'); // specify the type
$firstPhone = $user->getFirstPhone(); // get the first phone

方法和关系

HasPhones 方法与关系

使用 HasPhones 特性时可用的方法列表。

关系: phones(): MorphMany

获取排序后的电话关系。

关系: unorderedPhones(): MorphMany

获取无排序的电话关系。

方法: addPhone($number, $countries = [], $type = null): Phone

向模型添加电话号码。

方法: getPhones($type = null): Collection|Phone[]

获取与模型相关的电话号码。

方法: getFirstPhone($type = null): Phone

获取与模型相关的第一个电话号码。

测试

./vendor/bin/phpunit

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件