spinegar/sugar7wrapper
Laravel wrapper for SugarCRM 7 REST API. Provides a clean PHP client with authentication helpers and convenient methods for common CRM operations like querying and updating records, making Sugar 7 integration quicker and more maintainable.
Installation
composer require spinegar/sugar7wrapper
Add to config/app.php under providers:
Spinegar\Sugar7Wrapper\Sugar7WrapperServiceProvider::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Spinegar\Sugar7Wrapper\Sugar7WrapperServiceProvider"
Update .env with your SugarCRM credentials:
SUGARCRM_URL=https://your-sugar-instance.com
SUGARCRM_USERNAME=your_username
SUGARCRM_PASSWORD=your_password
First Use Case: Fetching a Record
use Spinegar\Sugar7Wrapper\Facades\Sugar7Wrapper;
$account = Sugar7Wrapper::get('Accounts', 1); // Fetch Account ID 1
dd($account);
CRUD Operations
// Create
$newAccount = Sugar7Wrapper::create('Accounts', [
'name' => 'New Company',
'description' => 'Test account'
]);
// Update
$updatedAccount = Sugar7Wrapper::update('Accounts', 1, [
'description' => 'Updated description'
]);
// Delete
Sugar7Wrapper::delete('Accounts', 1);
Querying with Filters
$accounts = Sugar7Wrapper::query('Accounts')
->filter(['name' => 'Test'])
->limit(10)
->get();
Relationships
$accountWithContacts = Sugar7Wrapper::get('Accounts', 1, ['contacts']);
Bulk Operations
$ids = [1, 2, 3];
$accounts = Sugar7Wrapper::getMultiple('Accounts', $ids);
Laravel Eloquent Integration Use the facade in Eloquent models for direct SugarCRM access:
class AccountModel extends Model {
public function fetchFromSugar($id) {
return Sugar7Wrapper::get('Accounts', $id);
}
}
Event Listeners Sync SugarCRM data on model events:
public function created(Account $account) {
Sugar7Wrapper::create('Accounts', $account->toArray());
}
API Resource Transformation Convert SugarCRM responses to Laravel API Resources:
public function toArray($request) {
$sugarData = Sugar7Wrapper::get('Accounts', $this->id);
return [
'name' => $sugarData['name'],
'description' => $sugarData['description']
];
}
Authentication Issues
.env credentials are correct and the SugarCRM REST API is enabled.Rate Limiting
try {
$data = Sugar7Wrapper::get('Accounts', 1);
} catch (\Exception $e) {
if ($e->getCode() === 429) {
sleep(2);
retry();
}
}
Field Mapping
account_name instead of name). Verify field names via SugarCRM’s REST API docs or the describe method:
$fields = Sugar7Wrapper::describe('Accounts');
Timeouts
$accounts = Sugar7Wrapper::query('Accounts')->limit(100)->get();
Enable Logging
Add to config/sugar7wrapper.php:
'debug' => env('APP_DEBUG', false),
Check logs for failed requests.
Raw Response Inspection
Use the raw() method to debug API responses:
$response = Sugar7Wrapper::get('Accounts', 1, [], true);
dd($response);
Custom Endpoints Extend the wrapper for non-standard SugarCRM endpoints:
Sugar7Wrapper::customRequest('GET', '/api/v11/rest.php', [
'method' => 'getEntryPoint',
'input' => ['module' => 'CustomModule']
]);
Middleware Add request/response middleware:
Sugar7Wrapper::withMiddleware(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Caching Cache frequent queries using Laravel’s cache:
$account = cache()->remember("sugar_account_{$id}", now()->addHours(1), function () use ($id) {
return Sugar7Wrapper::get('Accounts', $id);
});
How can I help you explore Laravel packages today?