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
return [
'models' => [
// Swap with your own model (e.g. to use UUIDs).
'address' => \Masterix21\Addressable\Models\Address::class,
],
'tables' => [
// Change before running the migration.
'addresses' => 'addresses',
],
// SRID used for the POINT column. 4326 = WGS84 (lat/lng).
'srid' => 4326,
// Template for the display_address accessor. Use {field_name} placeholders.
// Set to null to fall back to the default " - " separated format.
'display_format' => null,
];
use Masterix21\Addressable\Models\Concerns\HasAddresses;
class User extends Model
{
use HasAddresses;
}
$user->addresses; // MorphMany of Masterix21\Addressable\Models\Address
use Masterix21\Addressable\Models\Concerns\HasBillingAddresses;
use Masterix21\Addressable\Models\Concerns\HasShippingAddresses;
class User extends Model
{
use HasBillingAddresses, HasShippingAddresses;
}
$user->billingAddress; // Primary billing address (MorphOne)
$user->billingAddresses; // All billing addresses (MorphMany)
$user->shippingAddress; // Primary shipping address (MorphOne)
$user->shippingAddresses; // All shipping addresses (MorphMany)
// Generic address
$user->addAddress([
'label' => 'Home',
'street_address1' => 'Via Roma 1',
'zip' => '20100',
'city' => 'Milan',
'state' => 'MI',
'country' => 'IT',
]);
// Billing address — is_billing is set automatically
$user->addBillingAddress([
'street_address1' => 'Via Roma 1',
'city' => 'Milan',
]);
// Shipping address — is_shipping is set automatically
$user->addShippingAddress([
'street_address1' => 'Via Roma 1',
'city' => 'Milan',
]);
// Fetch the primary address (any type) via the eager-loadable relation
$user->primaryAddress; // ?Address
User::with('primaryAddress')->get(); // eager loaded
use Masterix21\Addressable\Models\Address;
Address::query()->primary()->get();
Address::query()->billing()->get();
Address::query()->shipping()->get();
// Scopes are composable
Address::query()->billing()->primary()->first();
$address->addressable; // The parent model (User, Company, ...)
use MatanYadaev\EloquentSpatial\Objects\Point;
$user->addBillingAddress([
'street_address1' => 'Via Antonio Izzi de Falenta, 7/C',
'zip' => '88100',
'city' => 'Catanzaro',
'state' => 'CZ',
'country' => 'IT',
'coordinates' => new Point(38.90852, 16.5894, config('addressable.srid')),
]);
// Or assign later
$billingAddress->coordinates = new Point(38.90852, 16.5894, config('addressable.srid'));
$billingAddress->save();
use MatanYadaev\EloquentSpatial\Objects\Point;
$milano = new Point(45.4391, 9.1906, config('addressable.srid'));
// Addresses within 10 km of Milan
Address::query()->withinRadius($milano, 10_000)->get();
$origin = new Point(45.4642, 9.1900, config('addressable.srid'));
// Default column name: `distance`
$addresses = Address::query()
->addDistanceTo($origin)
->get();
$addresses->first()->distance; // e.g. 1523.4
// Custom column name
Address::query()->addDistanceTo($origin, as: 'dist_meters')->get();
// Nearest first
Address::query()->addDistanceTo($origin)->orderBy('distance')->get();
$milano = new Point(45.4642, 9.1900, config('addressable.srid'));
// The 5 addresses closest to Milan, each with a populated `distance` (meters)
$closest = Address::query()->nearest($milano, 5)->get();
$closest->first()->distance; // e.g. 42.1
// Composable with any other scope
Address::query()->billing()->nearest($milano, 3)->get();
// Without a limit, ordering is applied but the result set is not truncated
Address::query()->shipping()->nearest($milano)->paginate(20);
// Ordering only, no `distance` column
Address::query()->orderByDistance($milano, 'desc')->get();
$address = $user->addAddress([
'street_address1' => 'Via Roma 1',
'zip' => '20100',
'city' => 'Milan',
'country' => 'IT',
]);
if ($address->geocode()) { // fills `coordinates`, does not persist
$address->save();
}
$address->coordinates = new Point(45.4642, 9.19, config('addressable.srid'));
if ($address->reverseGeocode()) { // fills street/zip/city/state/country
$address->save();
}
// With auto enabled, coordinates are resolved on save
$user->addAddress([
'street_address1' => 'Via Roma 1',
'city' => 'Milan',
'country' => 'IT',
]);
use Illuminate\Contracts\Queue\ShouldQueue;
use Masterix21\Addressable\Jobs\GeocodeAddressJob;
class QueuedGeocodeAddressJob extends GeocodeAddressJob implements ShouldQueue
{
}