chuckcms/laravel-contacts 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chuckcms/laravel-contacts

Composer 安装命令:

composer require chuckcms/laravel-contacts

包简介

An easy way for attaching contacts to Eloquent models in Laravel.

README 文档

README

Latest Version on Packagist Scrutinizer Code Quality StyleCI Total Downloads

An easy way for attaching contacts to Eloquent models in Laravel. Inspired by the following packages:

Installation

Require the package by running

composer require chuckcms/laravel-contacts

Publish configuration and migration

php artisan vendor:publish --provider="Chuckcms\Contacts\ContactsServiceProvider"

This command will publish a config/contacts.php and a migration file.

You can modify the default fields and their rules by changing both of these files.

After publishing you can run the migrations

php artisan migrate

Usage

You can use the HasContacts trait on any model.

<?php

namespace App\Models;

use Chuckcms\Contacts\Traits\HasContacts;
use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    use HasContacts;

    // ...
} 

After doing this you can use the following methods.

Create a new contact and attach it to a model

$company = Company::first();
$company->addContact([
	'first_name' 				=> 'John', // required
	'last_name' 				=> 'Doe', // defaults to: null
	'mobile' 				=> '+32.470123456', // defaults to: null
	'email' 				=> 'john.doe@email.com', // defaults to: null
	'is_public'				=> true, // defaults to: false
	'is_primary'				=> true, // defaults to: false
]);

Attach an existing contact to a model

use Chuckcms\Contacts\Models\Contact;

$contact = Contact::first();
$company = Company::first();

$company->assignContact($contact);

Update an existing contact

$company = Company::first();
$contact = $company->getPrimaryContact();

$company->updateContact($contact, ['middle_name' => 'In The Middle']);

Remove a contact from a model

$company = Company::first();
$contact = $post->contacts()->first();

$company->removeContact($contact);

Remove all contacts from a model and replace with given contacts (sync)

use Chuckcms\Contacts\Models\Contact;

$contacts = Contact::isPublic();
$company = Company::first();

$company->syncContacts($contacts);
// OR
$contacts = Contact::isPrimary()->pluck('id')->toArray();
$company->syncContacts($contacts);
// OR
$contact = Contact::isPrimary()->first();
$company->syncContacts($contact->id);

Delete a contact from any model it's linked to

$company = Company::first();
$contact = $company->contacts()->first();

$company->deleteContact($contact);

Determine if a model has any contacts

$company = Company::first();

if ($company->hasContacts()) {
	//do something
}

Determine if a model has (one of) the given contact(s).

use Chuckcms\Contacts\Models\Contact;

$company = Company::find();
$contact = Contact::first();

if ($company->hasContact($contact)) {
	//do something
}

//OR
if ($company->hasContact($contact->id)) {
	//do something
}

//OR
$contacts = Contact::where('first_name', 'John')->get();
if ($company->hasContact($contacts)) {
	//do something
}

//OR
$contacts = Contact::where('last_name', 'Doe')->pluck('id')->toArray();
if ($company->hasContact($contacts)) {
	//do something
}

This will return true when one of the given contacts belongs to the model.

Getters

You can use the following methods to retrieve contacts and certain attributes.

Get the public contact of the model.

$company = Company::first();

$publicContact = $company->getPublicContact();

Get the primary contact of the model.

$company = Company::first();

$primaryContact = $company->getPrimaryContact();

Get the first names of all contacts of the model.

$company = Company::first();

$first_names = $company->getContactFirstNames();

License

Licensed under MIT license.

Author

Written by Karel Brijs in Antwerp.

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固