PHP code example of justbetter / laravel-magento-client

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




class Example
{
    public function __construct(
        protected \JustBetter\MagentoClient\Client\Magento $magento,
    ) {
    }

    public function retrieveProduct()
    {
        $response = $this->magento->get('products/1');
    }

    public function retrieveOrdersLazily()
    {
        $retrievedOrders = [];

        $searchCriteria = \JustBetter\MagentoClient\Query\SearchCriteria::make()
            ->where('state', 'processing');

        foreach ($this->magento->lazy('orders', $searchCriteria->get()) as $order) {
            $retrievedOrders[] = $order['increment_id'];
        }
    }

    public function multipleConnections()
    {
        $this->magento->connection('connection_one')->get('products/1');
        $this->magento->connection('connection_two')->get('products/1');
    }
}


    'connection' => 'default',
    'connections' => [
        'default' => [
            /* Base URL of Magento, for example: https://magento.test */
            'base_url' => env('MAGENTO_BASE_URL'),

           // Other settings
        ],
        'another_connection' => [
            'base_url' => env('ANOTHER_MAGENTO_BASE_URL'),

            // Other settings
        ],
    ],

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->connection('connection_one')->get('products');



public function __construct(
    protected \JustBetter\MagentoClient\Client\Magento $magento
) {

}

// OR

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);



$search = \JustBetter\MagentoClient\Query\SearchCriteria::make()
        ->select('items[sku,name]')
        ->where('sku', '!=', '123')
        ->orWhere('price', '>', 10),
        ->whereIn('sku', ['123', '456'])
        ->orWhereNull('name')
        ->orderBy('sku')
        ->paginate(1, 50)
        ->get();

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->connection('connection_one')->get('products');
$client->connection('connection_two')->get('products');

/** @var \JustBetter\MagentoClient\Client\Magento $client */
$client = app(\JustBetter\MagentoClient\Client\Magento::class);

$client->graphql(
    'query searchProducts($search: String) {
        products(
            search: $search
        ) {
            items {
            sku
            }
        }
    }',
    [
        'search' => 'test'
    ]
);



Http::fake([
    '*/rest/all/V1/products*' => Http::response([
        'items' => [['sku' => '::some_sku::']],
        'total_count' => 1,
    ]),
]);
bash
php artisan vendor:publish --provider="JustBetter\MagentoClient\ServiceProvider" --tag=config