viitortest / contactable
提供联系信息,以便在一个地方存储多个联系人
dev-master
2020-04-29 10:48 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-26 07:43:39 UTC
README
Contacts是一个多态的Laravel包,用于联系人管理系统。您可以通过简单的方式将联系人添加到任何Eloquent模型中。
安装
-
通过composer安装此包
composer require viitortest/contactable
-
发布资源(迁移和配置文件)
php artisan vendor:publish --tag=contact-migrations php artisan vendor:publish --tag=contact-config
-
执行以下命令进行迁移
php artisan migrate
-
完成!
用法
要为您的Eloquent模型添加联系人支持,请简单地使用\Viitortest\Contacts\Traits\HasContacts特性。
管理您的联系人
// Get instance of your model $user = new \App\Models\User::find(1); // Create a single contact $user->addContact([ 'name' => 'Dhaval Joshi', 'email' => 'dhaval@example.com', 'phone' => '9999999999', ]); // Create a multiple contacts $user->addManyContacts( [ [ 'name' => 'Dhaval Joshi', 'email' => 'dhaval@example.com', 'phone' => '9999999999', ], [ 'name' => 'Shailesh Jakhaniya', 'email' => 'shailesh@example.com', 'phone' => '9999999999', ] ]); // Find an existing contact $contact = Contactable::find(1); // Update an existing contact $contact->update([ 'email' => 'iOmranic@gmail.com', ]); // Delete contact $contact->delete(); // Alternative way of contact deletion $user->contacts()->where('id', 123)->first()->delete(); // Get attached contacts collection $user->contacts; // Get attached contacts query builder $user->contacts();