PHP code example of masterix21 / laravel-addressable

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

    

masterix21 / laravel-addressable example snippets


use Masterix21\Addressable\Models\Concerns\HasBillingAddresses;
use Masterix21\Addressable\Models\Concerns\HasShippingAddresses;

class User extends Model {
    use HasBillingAddresses, 
        HasShippingAddresses;
}

$user->billingAddress(); // Primary billing address
$user->billingAddresses(); // All billing addresses

$user->shippingAddress(); // Primary shipment address
$user->shippingAddresses(); // All shipment addresses

$shippingAddress->markPrimary(); // It will emit the events `AddressPrimaryMarked` and `ShippingAddressPrimaryMarked`
$shippingAddress->unmarkPrimary(); // It will emit the events `AddressPrimaryUnmarked` and `ShippingAddressPrimaryUnmarked`

$billingAddress->markPrimary(); // It will emit the events `AddressPrimaryMarked` and `BillingAddressPrimaryMarked`
$billingAddress->unmarkPrimary(); // It will emit the events `AddressPrimaryUnmarked` and `BillingAddressPrimaryUnmarked`

$user->billingAddress()->create([
  'street_address1' => 'Via Antonio Izzi de Falenta, 7/C',
  'zip' => '88100',
  'city' => 'Catanzaro',
  'state' => 'CZ',
  'country' => 'Italy',
  'country_code' => 'IT',
  'coordinates' => new Point(16.0129, 36.01010)
]);

$billingAddress->coordinates = new Point(38.90852, 16.5894); 
$billingAddress->save();

// Take all addresses within 10 km
$addresses = Address::query()
    ->whereDistanceSphere(
        column: 'coordinates',
        geometryOrColumn: new Point(45.4391, 9.1906, config('addressable.srid')),
        operator: '<=',
        value: 10_000
    )
    ->get();

// Take all addresses over 10 km
$addresses = Address::query()
    ->whereDistanceSphere(
        column: 'coordinates',
        geometryOrColumn: new Point(45.4391, 9.1906, config('addressable.srid')),
        operator: '>=',
        value: 10_000
    )
    ->get();
bash
php artisan vendor:publish --provider="Masterix21\Addressable\AddressableServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Masterix21\Addressable\AddressableServiceProvider" --tag="config"