PHP code example of grnspc / addressable

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

    

grnspc / addressable example snippets


 

namespace App\Models;

use Grnspc\Addresses\Traits\HasAddresses;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasAddresses;

    // ...
}

    use App\Models\User;

    $user = User::find(1);
    $user->addAddress([
        'label'         => 'Default Address',
        'given_name'    => 'Nathan',
        'family_name'   => 'Robinson',
        'organization'  => 'GrnSpc',
        'line_1'        => '117 Banff Ave',
        'line_2'        => null,
        'city'          => 'Banff',
        'province'      => 'Alberta',
        'postal_code'   => 'T1L 1A4',
        'country_code'  => 'ca',
        'extra'         => [
                            'buzz_code' => '1234'    
                        ],
        'latitude'      => '51.1754012',
        'longitude'     => '-115.5715499',
        'is_primary'    => true,
        'is_billing'    => true,
        'is_shipping'   => true,
    ]);
    

    use App\Models\User;

    $user = User::find(1);
    $user->addresses()->create([
        'label'         => 'Default Address',
        'given_name'    => 'Nathan',
        'family_name'   => 'Robinson',
        'organization'  => 'GrnSpc',
        'line_1'        => '117 Banff Ave',
        'line_2'        => null,
        'city'          => 'Banff',
        'province'      => 'Alberta',
        'postal_code'   => 'T1L 1A4',
        'country_code'  => 'ca',
        'extra'         => [
                            'buzz_code' => '1234'    
                        ],
        'latitude'      => '51.1754012',
        'longitude'     => '-115.5715499',
        'is_primary'    => true,
        'is_billing'    => true,
        'is_shipping'   => true,
    ]);
    

    use App\Models\User;

    $user = User::find(1);
    $user->addresses()->createMany([
        [...],
        [...],
        [...],
    ]);
    

    $address = $user->addresses()->first();
    $newAttributes = [
        'label' => 'Default Work Address',
    ];
    $user->updateAddress($address, $newAttributes);
    

    $address = $user->addresses()->first();
    $address->update([
        'label' => 'Default Work Address',
    ]);
    

    $address = $user->addresses()->first();

    $user->deleteAddress($address);
    

    $address = $user->addresses()->first();
    $address->delete();
    

    $user->addresses()->firstWhere('id', 123)->delete();
    

    $user->flushAddresses();
    

    $user->addresses()->delete();
    

use Grnspc\Addresses\Facades\Address;

$addresses = Address::all();

if ($user->hasAddresses()) {
    // Do something
}

// Method 1 (Collection)
$addresses = $user->addresses;

// Method 2 (Collection)
$addresses = $user->addresses()->get();

// Method 3 (Query Builder)
$addresses = $user->addresses();

// if a model only has one address

// address in order: only1 > is_primary > latest 
$address = $user->address;

// billing address in order: only1 > is_billing > latest
$address = $user->billing_address;

// shipping address in order: only1 > is_shipping > latest
$address = $user->shipping_address;

// return all primary addresses
$addresses = Address::isPrimary()->get()

// return all primary addresses for a model
$addresses = $user->addresses->isPrimary()->get();

// return all billing addresses
$addresses = Address::isBilling()->get()

// return all billing addresses for a model
$addresses = $user->addresses->isBilling()->get();

// return all shipping addresses
$addresses = Address::isShipping()->get()

// return all shipping addresses for a model
$addresses = $user->addresses->isShipping()->get();

// return all addresses in country
$addresses = Address::InCountry('ca')->get()

$fiveKmAddresses = User::findByDistance(5, 'kilometers', '51.1754012', '-115.5715499')->get();

// Alternative method to find users within certain radius
$user = new \App\Models\User();
$users = $user->lat('51.1754012')->lng('-115.5715499')->within(5, 'kilometers')->get();
shell
    php artisan grnspc:publish:addresses
    
shell
    php artisan grnspc:migrate:addresses