PHP code example of justbetter / laravel-dynamics-client

1. Go to this page and download the library: Download justbetter/laravel-dynamics-client 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/ */

    

justbetter / laravel-dynamics-client example snippets


use JustBetter\DynamicsClient\Client\Dynamics;
use JustBetter\DynamicsClient\Data\Entity;
use JustBetter\DynamicsClient\Query\QueryBuilder;

$dynamics = app(Dynamics::class);

$customers = $dynamics->entities('customers', QueryBuilder::make()
    ->where('city', 'Alkmaar')
    ->orderBy('displayName')
    ->get());

$customer = $customers->first();

$customer->displayName = 'John Doe';

$dynamics->entity($customer)->update();

'base_url' => 'https://api.businesscentral.dynamics.com/v2.0/{tenant_id}/{environment}/api/{api}/companies({company_id})',
'token_url' => 'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token',

'parameters' => [
    'tenant_id' => env('DYNAMICS_TENANT_ID'),
    'environment' => env('DYNAMICS_ENVIRONMENT'),
    'api' => env('DYNAMICS_API', 'v2.0'),
    'company_id' => env('DYNAMICS_COMPANY_ID'),
],

$dynamics = app(Dynamics::class);

$dynamics
    ->tenantId('::tenant-id::')
    ->environment('Production')
    ->api('justbetter/general/v1.0')
    ->companyId('::company-id::');

// Any other placeholder, or several at once.
$dynamics->set('environment', 'Sandbox');
$dynamics->parameters(['environment' => 'Sandbox', 'api' => 'v2.0']);

$dynamics->reset();

'companies' => [
    'acme' => env('DYNAMICS_COMPANY_ACME_ID'),
    'other' => env('DYNAMICS_COMPANY_OTHER_ID'),
],

$dynamics->company('acme')->entities('customers');

$dynamics->connection('other')->entities('customers');

$response = $dynamics->get('customers', ['$top' => 10]);
$response = $dynamics->post('customers', ['displayName' => 'John Doe']);
$response = $dynamics->patch('customers(::id::)', ['displayName' => 'Jane Doe']);
$response = $dynamics->put('customers(::id::)', ['displayName' => 'Jane Doe']);
$response = $dynamics->delete('customers(::id::)');

$response->throw();

$dynamics->header('Prefer', 'return=representation')->post('customers', ['displayName' => 'John Doe']);

$customers = $dynamics->entities('customers');

$customer = $customers->first();

$customer->displayName;      // Attributes are accessed as properties
$customer->id();             // The "id" attribute
$customer->etag();           // The "@odata.etag" attribute
$customer->endpoint();       // "customers"
$customer->url();            // "customers(::id::)"

use JustBetter\DynamicsClient\Data\Entity;

$response = $dynamics->post('customers', ['displayName' => 'John Doe'])->throw();

$customer = Entity::from($response, ['endpoint' => 'customers']);

$customer->displayName = 'Jane Doe';

$dynamics->entity($customer)->update();

$dynamics->entity($customer)->update(['displayName' => 'Jane Doe']);

$dynamics->entity($customer)->delete();

$dynamics->entity($customer, 'customers')->update();

$dynamics->header('If-Match', $etag)->patch('customers(::id::)', ['displayName' => 'Jane Doe']);

$dynamics
    ->lazy('customers', ['$orderby' => 'id'])
    ->each(function (array $customer): void {
        //
    });

$dynamics->lazy('customers', ['$orderby' => 'id'], 100);

$dynamics
    ->lazyEntities('customers', ['$orderby' => 'id'])
    ->each(function (Entity $customer): void {
        //
    });

use JustBetter\DynamicsClient\Query\QueryBuilder;

$query = QueryBuilder::make()
    ->select(['id', 'number', 'displayName'])
    ->where('city', 'Alkmaar')
    ->where('balance', '>', 100)
    ->whereIn('number', ['1000', '2000'])
    ->whereNotNull('phoneNumber')
    ->orderByDesc('lastModifiedDateTime')
    ->take(50)
    ->get();

$customers = $dynamics->entities('customers', $query);

QueryBuilder::make()
    ->where('city', 'Alkmaar')
    ->where('balance', '>', 100)
    ->orWhere('blocked', 'All')
    ->get();

// $filter=city eq 'Alkmaar' and (balance gt 100 or blocked eq 'All')

use Illuminate\Support\Facades\Http;
use JustBetter\DynamicsClient\Client\Dynamics;

Dynamics::fake();

Http::fake([
    'dynamics/customers*' => Http::response([
        'value' => [
            [
                '@odata.etag' => '::etag::',
                'id' => '::id::',
                'displayName' => 'John Doe',
            ],
        ],
    ]),
]);

$customers = app(Dynamics::class)->entities('customers');
shell
php artisan vendor:publish --provider="JustBetter\DynamicsClient\ServiceProvider" --tag=config
shell
php artisan dynamics:connect {connection?}