cleaniquecoders / profile
常见配置信息
v5.0.0
2024-03-21 03:35 UTC
Requires
- php: ^8.1 | ^8.2 | ^8.3
- cleaniquecoders/blueprint-macro: ^5.0
- illuminate/auth: ^9.0 | ^10.0 | ^11.0
- illuminate/support: ^9.0 | ^10.0 | ^11.0
Requires (Dev)
- laravel/pint: ^1.6
- orchestra/testbench: 7.*|8.*
- pestphp/pest: ^2.0
- phpstan/phpstan-phpunit: ^1.2
- dev-master
- v5.0.0
- v4.1.1
- 4.1.0
- 4.0.0
- v3.1.3
- v3.1.2
- 3.1.1
- 3.1.0
- v3.0.0
- v2.0.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/github_actions/stefanzweifel/git-auto-commit-action-5
- dev-dependabot/github_actions/actions/checkout-4
This package is auto-updated.
Last update: 2024-09-21 04:45:39 UTC
README
Profile是一个使用多态方法存储基本信息的包 - 地址、电话号码、电子邮件和网站。
安装
- 为了在Laravel项目中安装Profile
$ composer require cleaniquecoders/profile
- 然后在你的
config/app.php
中将以下内容添加到providers
键中
\CleaniqueCoders\Profile\ProfileServiceProvider::class,
发布工厂文件
$ php artisan vendor:publish --tag=profile-factories
发布迁移文件
$ php artisan vendor:publish --tag=profile-migrations
发布种子文件
$ php artisan vendor:publish --tag=profile-seeds
然后运行
$ php artisan migrate
然后使用以下命令填充国家和电话类型数据
$ php artisan profile:seed
配置
现在你可以配置自己的模型和类型名称。请参阅config/profile.php
。
你可以在config/profile.php
中定义自己的profile:seed
种子器。
可用的多态特性
使用案例
- 一家公司有地址、电话号码、电子邮件和网站。
- 一名员工有地址、电话号码、电子邮件和网站。
这使我们使用多态来解决存储数据相似性的问题。
设置
可用的多态特性
CleaniqueCoders\Profile\Traits\Morphs\Addressable
CleaniqueCoders\Profile\Traits\Morphs\Emailable
CleaniqueCoders\Profile\Traits\Morphs\Phoneable
CleaniqueCoders\Profile\Traits\Morphs\Websiteable
CleaniqueCoders\Profile\Traits\Morphs\Bankable
对于实体的最常见设置是使用HasProfile
特性。
HasProfile
特性包含
CleaniqueCoders\Profile\Traits\Morphs\Addressable
CleaniqueCoders\Profile\Traits\Morphs\Emailable
CleaniqueCoders\Profile\Traits\Morphs\Phoneable
CleaniqueCoders\Profile\Traits\Morphs\Websiteable
namespace App; use CleaniqueCoders\Profile\Traits\HasProfile; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasProfile; }
使用方法
为每个配置创建记录
$user->addresses()->create([ 'primary' => '9 miles, Sungei Way', 'secondary' => 'P.O.Box 6503, Seri Setia', 'city' => 'Petaling Jaya', 'postcode' => '46150', 'state' => 'Selangor', 'country_id' => config('profile.providers.country.model')::name('Malaysia')->first()->id ]);
$user->phones()->create([ 'phone_number' => '+6089259167', 'is_default' => true, 'phone_type_id' => PhoneType::HOME, ]); $user->phones()->create([ 'phone_number' => '+60191234567', 'is_default' => true, 'phone_type_id' => PhoneType::MOBILE, ]); $user->phones()->create([ 'phone_number' => '+60380001000', 'is_default' => true, 'phone_type_id' => PhoneType::OFFICE, ]); $user->phones()->create([ 'phone_number' => '+60380001000', 'is_default' => true, 'phone_type_id' => PhoneType::OTHER, ]); $user->phones()->create([ 'phone_number' => '+60380001001', 'is_default' => true, 'phone_type_id' => PhoneType::FAX, ]); // you can futher query using local scopes in phone models. // get the first home phone number $user->phones()->home()->first(); // get all the mobile phone numbers $user->phones()->mobile()->get();
$user->emails()->create([...]); $user->websites()->create([...]); $user->bankable()->create([...]);
获取所有记录
$user->addresses; $user->emails; $user->phones; $user->websites; $user->banks;
许可证
本软件包是开源软件,许可协议为MIT许可。