masterix21/laravel-contacts
Composer 安装命令:
composer require masterix21/laravel-contacts
包简介
Add contacts book abilities to any Laravel project
README 文档
README
Attach a contact book to any Eloquent model. Users, companies, venues, projects — anything that needs phone numbers, emails, websites, or social handles can grow them through a single polymorphic relation, without you spinning up a dedicated table for every model.
Why
Most apps end up scattering contact fields across half a dozen tables: a phone column on users, an email on companies, a separate addresses table that nobody trusts. This package centralises all of that into one contacts table and lets any model attach as many entries as it needs.
A single contact row can hold a label (e.g. Office, Personal), a phone, a mobile, an email, a website, a few social handles, a push token, and a freeform meta JSON payload for anything else.
Requirements
- PHP 8.2+
- Laravel 11, 12, or 13
Installation
Install via Composer:
composer require masterix21/laravel-contacts
Publish and run the migration:
php artisan vendor:publish --tag="laravel-contacts-migrations"
php artisan migrate
If you need to swap the Contact model for your own subclass, publish the config:
php artisan vendor:publish --tag="laravel-contacts-config"
// config/contacts.php return [ 'models' => [ 'contact' => \App\Models\Contact::class, ], ];
Usage
Add the HasContacts trait to any model that should own contacts:
use Illuminate\Database\Eloquent\Model; use LucaLongo\LaravelContacts\Models\Concerns\HasContacts; class User extends Model { use HasContacts; }
That's it — the model now exposes a polymorphic contacts() relation plus four filtered helpers.
Adding contacts
$user->contacts()->create([ 'label' => 'Office', 'email' => 'luca@example.com', 'phone' => '+39 02 1234567', 'website' => 'https://example.com', ]); $user->contacts()->create([ 'label' => 'Personal', 'mobile' => '+39 333 1234567', 'meta' => [ 'preferred_channel' => 'whatsapp', 'timezone' => 'Europe/Rome', ], ]);
Reading contacts
The trait ships with helpers that filter the relation by the field you care about:
$user->contacts; // every contact $user->emails; // only contacts with a non-null email $user->phones; // only contacts with a non-null phone $user->mobiles; // only contacts with a non-null mobile $user->websites; // only contacts with a non-null website
Each helper returns a MorphMany, so you can keep chaining:
$primaryEmail = $user->emails()->where('label', 'Office')->first()?->email;
The meta field
meta is cast to AsArrayObject, so you can read and write it like a native array and Laravel will persist the JSON for you:
$contact = $user->contacts()->first(); $contact->meta['preferred_channel'] = 'email'; $contact->save();
Available fields
| Field | Type | Notes |
|---|---|---|
label |
string | Free label, e.g. Office, Billing |
phone, mobile |
string | Landline / mobile numbers |
email |
string | |
website |
string | |
facebook, x, linkedin |
string | Social handles or full URLs |
push_token |
string | Device push token |
meta |
array | Anything else, stored as JSON |
The model has no $fillable and $guarded = [], so mass-assignment is open by design — guard your input at the request layer.
Testing
composer test
Changelog
See CHANGELOG for the release history.
Contributing
See CONTRIBUTING.
Security
Found a vulnerability? Please review the security policy before opening a public issue.
Credits
License
The MIT License (MIT). See LICENSE.md.
masterix21/laravel-contacts 适用场景与选型建议
masterix21/laravel-contacts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 146 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「LucaLongo」 「laravel-contacts」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 masterix21/laravel-contacts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 masterix21/laravel-contacts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 masterix21/laravel-contacts 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Add bookings ability to any Eloquent model
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
统计信息
- 总下载量: 146
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-23