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.
It uses the great package matanyadaev/laravel-eloquent-spatial
by Matan Yadaev.
If you like my work, you can sponsoring me.
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"
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
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`
$user->billingAddress()->create([
'street_address1' => 'Via Antonio Izzi de Falenta, 7/C',
'zip' => '88100',
'city' => 'Catanzaro',
'state' => 'CZ',
'country' => 'Italy',
'country_code' => 'IT',
'position' => new Point(16.0129, 36.01010)
]);
$billingAddress->position = 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();
composer test
- Method to retrieve all nearby addresses of X kilometers
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.