1. Go to this page and download the library: Download wazza/sync-model-to-crm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wazza / sync-model-to-crm example snippets
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\Entity;
use Illuminate\Database\Eloquent\SoftDeletes;
use Wazza\SyncModelToCrm\Http\Controllers\CrmProviders\HubSpotController;
use Wazza\SyncModelToCrm\Traits\crmTrait;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;
// include this if you wish to use the `Mutators function` or
// $this->syncToCrm() directly as appose to the observer method
use crmTrait;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
/**
* Function that will be used to return the relationship data
* @return type
*/
public function entity()
{
return $this->belongsTo(Entity::class)->withTrashed();
}
// --------------------------------------------------------------
// Sync Model to CRM
// --------------------------------------------------------------
/**
* The CRM provider environment/s to use (e.g. production, sandbox, etc.)
* Use an array to sync to multiple environments.
* `null` (or not defined) will take the default from the config file.
*
* @var string|array|null
*/
public $syncModelCrmEnvironment = ['sandbox']; // ..or ['sandbox','production']
/**
* Mapping array for local and CRM properties
* This will be the primary property used to cycle through the crm providers
* and properties to sync the model to the CRM.
* Required - if not provided, the sync will process will be skipped (no Exceptions will be thrown)
*
* @var array
*/
public $syncModelCrmPropertyMapping = [
'hubspot' => [
'name' => 'firstname',
'email' => 'email',
// ... add all the properties that you would like to sync
],
];
/**
* Unique filters for the CRM to locate the record if there is no internal mapping available.
* Not the template examples in the sync-model-to-crm repo for a observer working copy
* \App\Models\User::observe(\App\Observers\UserObserver::class);
* }
*/
/**
* (2) Mutators function (Laravel 5.4 or above)
*
* Laravel provides mutators which are methods that can be defined on a model to modify
* attributes before they are saved. You can create a custom mutator named save that
* first calls the original save method using parent::save() and then performs your
* additional action.
*
* @param array $options
* @return void
*/
public function save(array $options = [])
{
parent::save($options);
// lets call the syncModelToCrm method to sync the model to the CRM.
// refer to the trait for all the available methods
// $this->syncToCrmPatch(); -- disabled as we are currently using the observer method
}
}
namespace App\Observers;
use App\Models\User;
use Wazza\SyncModelToCrm\Http\Controllers\CrmController;
use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
/**
* Register the observer in the AppServiceProvider boot method
*
* public function boot(): void
* {
* // register the observer/s
* \App\Models\User::observe(\App\Observers\UserObserver::class);
* }
*/
class UserObserver implements ShouldHandleEventsAfterCommit
{
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
echo ('create...');
(new CrmController())
->setModel($user)
->setAttemptCreate()
->execute(true);
echo ('created...');
}
/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
echo ('update...');
(new CrmController())
->setModel($user)
->setAttemptUpdate()
->execute(true);
echo ('updated...');
}
/**
* Handle the User "deleted" event.
* Run when a user is soft-deleted.
*/
public function deleted(User $user)
{
echo ('delete...');
(new CrmController())
->setModel($user)
->setAttemptDelete()
->execute();
echo ('deleted...');
}
/**
* Handle the User "restored" event.
* Soft-delete has been reversed.
*/
public function restored(User $user): void
{
echo ('restore...');
(new CrmController())
->setModel($user)
->setAttemptRestore()
->execute();
echo ('restored...');
}
/**
* Handle the User "force deleted" event.
*/
public function forceDeleted(User $user): void
{
echo ('forceDeleted...');
}
/**
* Handle the User "saved" event.
*
*/
public function saved(User $user): void
{
echo ('saving...');
(new CrmController())
->setModel($user)
->setAttemptAll() // open for anything...
->execute();
echo ('saved...');
}
}
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
Wazza\SyncModelToCrm\Providers\SyncModelToCrmServiceProvider::class, // <-- here
Wazza\DomTranslate\Providers\DomTranslateServiceProvider::class,
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
])->toArray(),