GrnSpc Addresses is a polymorphic Laravel package, for addressbook management. You can add addresses to any eloquent model with ease.
-
Install the package via composer:
composer require grnspc/addresses
-
Publish resources (migrations and config files):
php artisan grnspc:publish:addresses
-
Execute migrations via the following command:
php artisan grnspc:migrate:addresses
-
Done!
To add addresses support to your eloquent models simply use \Grnspc\Addresses\Traits\HasAddress
trait.
<?php
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);
-
Alternative way of address deletion
$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();
The API is intuitive and very straight forward, so let's give it a quick look:
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();
Refer to the Changelog for a full history of the project.
Thank you for considering contributing to this project! The contribution guide can be found in CONTRIBUTING.md.
Bug reports, feature requests, and pull requests are very welcome.
If you discover a security vulnerability within this project, please submit an issue. All security vulnerabilities will be promptly addressed.
This software is released under The MIT License (MIT).
(c) 2014-2022 GrnSpc, Some rights reserved.