Skip to content

masterix21/laravel-addressable

Repository files navigation

Addresses for any Eloquent model

MIT License Latest Version GitHub Tests Action Status Total Downloads

This package adds to any Eloquent model the addresses: in this way will be easier to support a billing address, the shipment addresses or others.

Support us

If you like my work, you can sponsoring me.

Installation

You can install the package via composer:

composer require masterix21/laravel-addressable

You can publish and run the migrations with:

php artisan vendor:publish --provider="Masterix21\Addressable\AddressableServiceProvider" --tag="migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Masterix21\Addressable\AddressableServiceProvider" --tag="config"

Usage

Extends an Eloquent model to supports the addresses is simple.

use Masterix21\Addressable\Models\Concerns\HasAddresses;

class User extends Model {
    use HasAddresses;
}

$user->shipments(); // morphMany of `Masterix21\Addressable\Models\Address` 

HasAddress is a generic trait that will implements all addresses code, but if you like to handle the shipments addresses or the billing address there are other two traits.

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

Mark and unmark an address as primary

To be sure that only one address per type will be "primary", you can use the markPrimary() method. It will mark the address as primary and will unmark the others (of the same type).

$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`

Create a billing address with position

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

Store latitude and longitude for an address

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

Query addresses by their distance from/to another point

// Take all addresses within 10 km
$addresses = Address::query()
  ->withPositionDistance(new Point(38.90852, 16.5894), 10000)
  ->get();

// Take all addresses over 10 km
$addresses = Address::query()
  ->withPositionDistance(new Point(38.90852, 16.5894), 10000, '>=')
  ->get();

Testing

composer test

Todo

  • Method to retrieve all nearby addresses of X kilometers

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.