承接 stechstudio/laravel-hubspot 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

stechstudio/laravel-hubspot

Composer 安装命令:

composer require stechstudio/laravel-hubspot

包简介

A Laravel SDK for the HubSpot CRM Api

README 文档

README

Latest Version on Packagist

Interact with HubSpot's CRM with an enjoyable, Eloquent-like developer experience.

  • Familiar Eloquent CRUD methods create, find, update, and delete
  • Associated objects work like relations: Deal::find(555)->notes and Contact::find(789)->notes()->create(...)
  • Retrieving lists of objects feels like a query builder: Company::where('state','NC')->orderBy('custom_property')->paginate(20)
  • Cursors provide a seamless way to loop through all records: foreach(Contact::cursor() AS $contact) { ... }

Note Only the CRM API is currently implemented.

Installation

1) Install the package via composer:

composer require stechstudio/laravel-hubspot

2) Configure HubSpot

Create a private HubSpot app and give it appropriate scopes for what you want to do with this SDK.

Copy the provided access token, and add to your Laravel .env file:

HUBSPOT_ACCESS_TOKEN=XXX-XXX-XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Usage

Individual CRUD actions

Retrieving

You may retrieve single object records using the find method on any object class and providing the ID.

use STS\HubSpot\Crm\Contact;

$contact = Contact::find(123);

Creating

To create a new object record, use the create method and provide an array of properties.

$contact = Contact::create([
    'firstname' => 'Test',
    'lastname' => 'User',
    'email' => 'testuser@example.com'
]);

Alternatively you can create the class first, provide properties one at a time, and then save.

$contact = new Contact;
$contact->firstname = 'Test';
$contact->lastname = 'User';
$contact->email = 'testuser@example.com';
$contact->save();

Updating

Once you have retrieved or created an object, update with the update method and provide an array of properties to change.

Contact::find(123)->update([
    'email' => 'newemail@example.com'
]);

You can also change properties individually and then save.

$contact = Contact::find(123);
$contact->email = 'newemail@example.com';
$contact->save();

Deleting

This will "archive" the object in HubSpot.

Contact::find(123)->delete();

Retrieving multiple objects

Fetching a collection of objects from an API is different from querying a database directly in that you will always be limited in how many items you can fetch at once. You can't ask HubSpot to return ALL your contacts if you have thousands.

This package provides three different ways of fetching these results.

Paginating

Similar to a traditional database paginated result, you can paginate through a HubSpot collection of objects. You will receive a LengthAwarePaginator just like with Eloquent, which means you can generate links in your UI just like you are used to.

$contacts = Contact::paginate(20);

By default, this paginate method will look at the page query parameter. You can customize the query parameter key by passing a string as the second argument.

Cursor iteration

You can use the cursor method to iterate over the entire collection of objects. This uses lazy collections and generators to seamlessly fetch chunks of records from the API as needed, hydrating objects when needed, and providing smooth iteration over a limitless number of objects.

// This will iterate over ALL your contacts!
foreach(Contact::cursor() AS $contact) {
    echo $contact->id . "<br>";
}

Warning API rate limiting can be an obstacle when using this approach. Be careful about iterating over huge datasets very quickly, as this will still require quite a few API calls in the background.

Manually fetching chunks

Of course, you can grab collections of records with your own manual pagination or chunking logic. Use the take and after methods to specify what you want to grab, and then get.

// This will get 100 contact records, starting at 501
$contacts = Contact::take(100)->after(500)->get();

// This will get the default 50 records, starting at the first one
$contacts = Contact::get();

Searching and filtering

When retrieving multiple objects, you will frequently want to filter, search, and order these results. You can use a fluent interface to build up a query before retrieving the results.

Adding filters

Use the where method to add filters to your query. You can use any of the supported operators for the second argument, see here for the full list: https://developers.hubspot.com/docs/api/crm/search#filter-search-results;

This package also provides friendly aliases for common operators =, !=, >, >=, <, <=, exists, not exists, like, and not like.

Contact::where('lastname','!=','Smith')->get();

You can omit the operator argument and = will be used.

Contact::where('email', 'johndoe@example.com')->get();

For the BETWEEN operator, provide the lower and upper bounds as a two-element tuple.

Contact::where('days_to_close', 'BETWEEN', [30, 60])->get();

Note All filters added are grouped as "AND" filters, and applied together. Optional "OR" grouping is not yet supported.

Searching common properties

HubSpot supports searching through certain object properties very easily. See here for details:

https://developers.hubspot.com/docs/api/crm/search#search-default-searchable-properties

Specify a search parameter with the search method:

Contact::search('1234')->get();

Ordering

You can order the results with any property.

Contact::orderBy('lastname')->get();

The default direction is asc, you can change this to desc if needed.

Contact::orderBy('days_to_close', 'desc')->get();

Associations

HubSpot associations are handled similar to Eloquent relationships.

Dynamic properties

You can access associated objects using dynamic properties.

foreach(Company::find(555)->contacts AS $contact) {
    echo $contact->email;
}

Association methods

If you need to add additional constraints, use the association method. You can add any of the filtering, searching, or ordering methods described above.

Company::find(555)->contacts()
    ->where('days_to_close', 'BETWEEN', [30, 60])
    ->search('smith')
    ->get();

Eager loading association IDs

Normally, there are three HubSpot API calls to achieve the above result:

  1. Fetch the company object
  2. Retrieve all the contact IDs that are associated to this company
  3. Query for contacts that match the IDs

Now we can eliminate the second API call by eager loading the associated contact IDs. This library always eager-loads the IDs for associated companies, contacts, deals, and tickets. It does not eager-load IDs for engagements like emails and notes, since those association will tend to be much longer lists.

If you know in advance that you want to, say, retrieve the notes for a contact, you can specify this up front.

// This will only be two API calls, not three
Contact::with('notes')->find(123)->notes;

Creating associated objects

You can create new records off of the association methods.

Company::find(555)->contacts()->create([
    'firstname' => 'Test',
    'lastname' => 'User',
    'email' => 'testuser@example.com'
]);

This will create a new contact, associate it to the company, and return the new contact.

You can also associate existing objects using attach. This method accepts and ID or an object instance.

Company::find(555)->contacts()->attach(Contact::find(123));

You can also detach existing objects using detach. This method accepts and ID or an object instance.

Company::find(555)->contacts()->detach(Contact::find(123));

License

The MIT License (MIT). Please see License File for more information.

stechstudio/laravel-hubspot 适用场景与选型建议

stechstudio/laravel-hubspot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 80.08k 次下载、GitHub Stars 达 30, 最近一次更新时间为 2022 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「hubspot」 「stechstudio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 stechstudio/laravel-hubspot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stechstudio/laravel-hubspot 我们能提供哪些服务?
定制开发 / 二次开发

基于 stechstudio/laravel-hubspot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 80.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 3
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-19