nunomazer / laravel-addressable
Laravel Addressable is a polymorphic Laravel package, for addressbook management. You can add addresses to any eloquent model with ease.
Requires
- php: ^7.3.0 || ^8.0.0
- illuminate/console: ^8.0.0 || ^9.0.0
- illuminate/database: ^8.0.0 || ^9.0.0
- illuminate/support: ^8.0.0 || ^9.0.0
- jackpopp/geodistance: ^1.2.0
- rinvex/countries: ^7.0.0
- rinvex/laravel-support: ^5.0.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.30.0
- illuminate/container: ^8.0.0 || ^9.0.0
- phpunit/phpunit: ^9.5.0
README
It is a polymorphic Laravel package, for addressbook management. You can add addresses to any eloquent model with ease.
This is based on Rinvex Addresses package
Installation
-
Install the package via composer:
composer require nunomazer/laravel-addressable
-
Publish resources (migrations and config files):
php artisan vendor:publish
-
Edit config file
config/addressable.php
if you need to change database table name:// Addresses Database Tables 'tables' => [ 'addresses' => 'addresses', ],
-
Execute migrations via the following command:
php artisan migrate
-
Done!
Usage
To add addresses support to your eloquent models simply use \NunoMazer\Addressable\Traits\Addressable
trait.
<?php namespace App\Models; ... use NunoMazer\Addressable\Traits\Addressable; class User extends Model { use Addressable;
Manage your addresses
// Get instance of your model $user = new \App\Models\User::find(1); // Create a new address // note that this are fake informations $user->addresses()->create([ 'label' => 'Default Address', 'organization' => 'DN42', 'country_code' => 'br', 'state' => 'ParanĂ¡', 'city' => 'Ponta Grossa', 'line_1' => 'Av. Carlos Cavalcanti', 'number' => '200', 'complement' => 'Apt #44', 'postal_code' => '84000-100', 'extra' => [ 'ibge_code' => '300' ], 'latitude' => '31.2467601', 'longitude' => '29.9020376', 'is_primary' => true, 'is_billing' => true, 'is_shipping' => true, ]); // Create multiple new addresses $user->addresses()->createMany([ [...], [...], [...], ]); // Find an existing address $address = app('addressable.address')->find(1); // Update an existing address $address->update([ 'label' => 'Default Work Address', ]); // Delete address $address->delete(); // Alternative way of address deletion $user->addresses()->where('id', 123)->first()->delete();
Manage your addressable model
The API is intuitive and very straight forward, so let's give it a quick look:
// Get instance of your model $user = new \App\Models\User::find(1); // Get attached addresses collection $user->addresses; // Get attached addresses query builder $user->addresses(); // Scope Primary Addresses $primaryAddresses = app('addressable.address')->isPrimary()->get(); // Scope Billing Addresses $billingAddresses = app('addressable.address')->isBilling()->get(); // Scope Shipping Addresses $shippingAddresses = app('addressable.address')->isShipping()->get(); // Scope Addresses in the given country $egyptianAddresses = app('addressable.address')->InCountry('eg')->get(); // Find all users within 5 kilometers radius from the latitude/longitude 31.2467601/29.9020376 $fiveKmAddresses = \App\Models\User::findByDistance(5, 'kilometers', '31.2467601', '29.9020376')->get(); // Alternative method to find users within certain radius $user = new \App\Models\User(); $users = $user->lat('31.2467601')->lng('29.9020376')->within(5, 'kilometers')->get();
Changelog
Refer to the Changelog for a full history of the project.
Support
The following support channels are available at your fingertips:
Contributing & Protocols
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.
Security Vulnerabilities
If you discover a security vulnerability within this project, please open an issue.
License
This software is released under The MIT License (MIT).