PHP code example of coderatio / ngstates

1. Go to this page and download the library: Download coderatio/ngstates 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/ */

    

coderatio / ngstates example snippets


// If you downloaded the library using composer
ser.
oderatio\NGStates\NGStates(); 
/* 
* Note: You can use the helper function which is an instance of NGStates::class. 
* e.g ngstates()->getStates();
*/ 

print_r($ngStates->getStates()); // Print all the states
exit;

// Get states
$ngStates->getStates();

// This will return an array of all states.

// Get State
$ngStates->getState(string|int $stateNameOrId);

// e.g
$ngStates->getState(26); 

// OR
$ngStates->getState('Nasarawa State');

// Get state local governments
$ngStates->getStateLocals(string|int $stateNameOrId);

// e.g
$ngStates->getStateLocals(26);

// OR
$ngStates->getStateLocals('Nasarawa State');

// Get state local government
$ngState->getStateLocal(string|int $stateNameOrId, string|int $localNameOrId);

// e.g
$ngStates->getStateLocal(26, 1);

// OR
$ngStates->getStateLocal('Nasarawa State', 'Lafia');

// Add multiple states to database
$ngStates->addStates(array $statesData);

// e.g
$ngStates->addStates([
    ['state' => [
        'id' => 38,
        'name' => 'Demo State'
        'locals' => [
            'id' => 1,
            'name' => 'Demo LGA One'
        ]
    ]],
    ['state' => [
        'id' => 39,
        'name' => 'Demo State Two'
        'locals' => [
            'id' => 1,
            'name' => 'Demo LGA One'
        ]
    ]],
]);


// Add a state
$ngStates->addState(array $stateData);

// e.g
$ngStates->addState([
  'id' => 38,
  'name' => 'Demo State',
  'locals' => [
    [
      'id' => 1,
      'name' => 'Demo LGA One'
    ],
    [
      'id' => 2,
      'name' => 'Demo LGA Two',
    ]
  ]
]);

// Add state local governments
$ngStates->addStateLocals(string|int $stateNameOrId, array $localsData);

// e.g
$ngStates->addStateLocals(38, [
    [
        'id' => 3,
        'name' => 'Demo LGA Three',
    ],
    [
        'id' => 4,
        'name' => 'Demo LGA Four'
   ]
]);

// Add single local government
$ngStates->addStateLocal(string|int $stateNameOrId, array $localData);

// e.g
$ngStates->addStateLocal(38, [
    'id' => 5,
    'name' => 'Demo LGA Five'
]);

// Update state
$ngStates->updateState(string|int $stateNameOrId, array $stateData);

// e.g
$ngStates->updateState(38, [
    'name' => 'Demo State Edited',
    'locals' => [
        [
            'id' => 1,
            'name' => 'Demo LGA One Edited'
        ]
    ]
]);

// Update state local governments
$ngStates->updateStateLocals(string|int $stateNameOrId, array $localsData);

// e.g
$ngStates->updateStateLocals(38, [
    [
        'id' => 1,
        'name' => 'Demo LGA One Updated'
    ],
    [
        'id' => 2,
        'name' => 'Demo LGA Two Updated'
    ]
]);


// Update state local
$ngStates->updateStateLocal(string|int $stateNameOrId, array $localData);

// e.g
$ngStates->updateStateLocal(38, [
    [
        'id' => 1,
        'name' => 'Demo LGA Changed'
    ]
]);

// Delete state
$ngStates->deleteState(int $stateNameOrId);

// e.g
$ngStates->deleteState(38);

// Delete state local government
$ngStates->deleteStateLocal(string|int $stateNameOrId, int $stateLocalId);

// e.g
$ngStates->deleteStateLocal(38, 1); // Will delete local government with the ID 1.