PHP code example of sholihin / rajaongkirprolaravel

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

    

sholihin / rajaongkirprolaravel example snippets


'providers' => [
	....
	
	sholihin\rajaongkirprolaravel\RajaOngkirServiceProvider::class,
]

'aliases' => [
	....
	
	'RajaOngkir' => sholihin\rajaongkirprolaravel\RajaOngkirFacade::class,
]

'end_point_api' => 'isi_base_url_api_akun_anda_disini',
'api_key' => 'isi_api_key_anda_disini',

Route::get('/get-provinces', 'ShippingController@getProvinces')->name('get.province');
Route::get('/generate-provinces', 'ShippingController@generateProvinces')->name('generate.province');
Route::get('/get-cities', 'ShippingController@getCities')->name('get.cities');
Route::get('/generate-cities', 'ShippingController@generateCities')->name('generate.cities');
Route::get('/get-city-by-province/{id}', 'ShippingController@getCityByProvince')->name('get.city.by.province');
Route::get('/get-subdistrict-by-city/{id}', 'ShippingController@getSubdistrictByID');
Route::get('/get-subdistrict-by-name/{name}', 'ShippingController@getSubdistrictByName');
Route::get('/generate-subdistrict', 'ShippingController@generateSubdistricts')->name('generate.subdistrict');
Route::get('/get-cost/{origin}/{destination}/{weight}/{courier}', 'ShippingController@getCost')->name('get.cost');



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use RajaOngkir;
use Illuminate\Support\Facades\DB;
use App\Models\Province;
use App\Models\City;
use App\Models\Subdistrict;

class ShippingController extends Controller
{
    public function getProvinces(){
        $data = RajaOngkir::Provinsi()->all();
        return $data;
    }

    public function getProvinceByName($param){
        $data = RajaOngkir::Provinsi()->search('province', $name = $param)->get();
        return $data;
    }

    public function generateProvinces(){
        $datas = RajaOngkir::Provinsi()->all();
        foreach($datas as $x){
            DB::table('provinces')->insert(
                ['province_id' => $x['province_id'], 'province' => $x['province']]
            );
            echo $x['province'].'<br>';
        }
    }

    public function getCities(){
        $data = RajaOngkir::Kota()->all();
        return $data;
    }

    public function getCityByName($param){
        $data = RajaOngkir::Kota()->search('city_name', $name = $param)->get();
        return $data;
    }

    public function getCityByProvince($id){
        $data = City::where('province_id', $id)->get();
        return $data;
    }
    
    //Jika ingin menyalin ke database lokal (perlu membuat tabel `cities`)
    public function generateCities(){
        $datas = RajaOngkir::Kota()->all();
        foreach($datas as $x){
            DB::table('cities')->insert([
                'city_id' => $x['city_id'],
                'province_id' => $x['province_id'], 
                'type' => $x['type'], 
                'city_name' => $x['city_name'], 
                'postal_code' => $x['postal_code']
            ]);
            echo $x['city_name'].'<br>';
        }
    }

    public function getSubdistrictByName($nama){
        $query = Subdistrict::where('subdistrict_name','like', '%' . $nama . '%')->get();
        $data = array();
        foreach($query as $x){
            $original_data = array('id'=>$x->subdistrict_id, 'value'=>$x->subdistrict_name.', '.$x->type.' '.$x->city.' - '.$x->province);
            array_push($data, $original_data);
        }
        return $data;
    }

    public function getSubdistrictByID($id){
        $data = RajaOngkir::Kecamatan()->byCity($id)->get();
        return $data;
    }

    //Jika ingin menyalin ke database lokal (perlu membuat tabel `subdistricts`)
    public function generateSubdistricts(){
        $datas = City::all();
        foreach($datas as $x){
            $dataDis = $this->getSubdistrictByID($x->city_id);
            foreach($dataDis as $insDis){
                DB::table('subdistricts')->insert([
                    'subdistrict_id' => $insDis['subdistrict_id'],
                    'province_id' => $insDis['province_id'],
                    'province' => $insDis['province'],
                    'city_id' => $insDis['city_id'],
                    'city' => $insDis['city'],
                    'type' => $insDis['type'],
                    'subdistrict_name' => $insDis['subdistrict_name']
                ]);

                // echo $insDis['subdistrict_name'].'<br>';
            }
            echo $x['city_name'].'<br>';
        }
    }
    
    public function getCost($origin, $destination, $weight, $courier){
        $data = RajaOngkir::Cost([
            'origin' 		    => $origin,
            'originType' 	    => 'subdistrict',
            'destination' 	    => $destination,
            'destinationType'   => 'subdistrict',
            'weight' 		    => $weight,
            'courier' 		    => $courier,
        ])->get();

        return $data;
    }
}

$data = RajaOngkir::Provinsi()->all();

$data = RajaOngkir::Provinsi()->count();

$data = RajaOngkir::Provinsi()->find($id);

$data = RajaOngkir::Provinsi()->search('province', $name = "ja")->get();

$data = RajaOngkir::Kota()->all();

$data = RajaOngkir::Kota()->count();

$data = RajaOngkir::Kota()->find($id);

$data = RajaOngkir::Kota()->search('city_name', $name = "banyu")->get();

$data = RajaOngkir::Kota()->byProvinsi($provinsi_id)->get();

$data = RajaOngkir::Kota()->byProvinsi($provinsi_id)->count();

$data = RajaOngkir::Kota()->byProvinsi($provinsi_id)->search('city_name', $name)->get();

$data = RajaOngkir::Kecamatan()->byCity($city_id)->get();

$data = RajaOngkir::Cost([
	'origin' 		=> 501,
	'originType' 		=> 'subdistrict',
	'destination' 		=> 574,
	'destinationType'	=> "subdistrict",
	'weight'		=> 1700,
	'courier'		=> 'jne'
])->get();

php artisan vendor:publish